-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP: Support basic data parallel #366
Open
shendiaomo
wants to merge
8
commits into
develop
Choose a base branch
from
basic_data_parallel
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
38155f8
Support basic data parallel
shendiaomo c907c41
Call Go `Module.Forward` from C++
shendiaomo 789e461
Fix lint
shendiaomo 784a3b7
Add test case
shendiaomo 91ad9da
Add nilness check and KeepAlive, derive from Module
shendiaomo 2140b09
Fix lint
shendiaomo 3007a5a
Fix syntax error and CI
shendiaomo beb37fd
Fix pointer problems
shendiaomo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright 2020, GoTorch Authors | ||
#ifdef WITH_CUDA | ||
#include <torch/nn/parallel/data_parallel.h> | ||
#endif | ||
|
||
#include <memory> | ||
|
||
#include "cgotorch/parallel.h" | ||
|
||
typedef Tensor (*ForwardMethod)(void *, Tensor); | ||
|
||
// goModule wraps the `goModuleForward` funciton defined in nn/parallel.go into | ||
// a class method | ||
struct goModule : torch::nn::Module { | ||
char *m_; | ||
ForwardMethod f_; | ||
goModule(char *m, void *f) : m_(m), f_(reinterpret_cast<ForwardMethod>(f)) {} | ||
at::Tensor forward(at::Tensor input) { // NOLINT: include_what_you_use | ||
// TODO(shendiaomo): check the return value of `f_` | ||
return *f_(m_, &input); | ||
} | ||
}; | ||
|
||
const char *DataParallel(char *go_module, void *f, Tensor input, | ||
Device *devices, int64_t size, Device *output, | ||
int64_t dim) { | ||
#ifdef WITH_CUDA | ||
try { | ||
if (input == nullptr) { | ||
throw std::runtime_error( | ||
"invalid memory address or nil pointer dereference of input tensor"); | ||
} | ||
torch::nn::parallel::data_parallel(std::make_shared<goModule>(go_module, f), | ||
*input); | ||
return nullptr; | ||
} catch (const std::exception &e) { | ||
return exception_str(e.what()); | ||
} | ||
#else | ||
return exception_str( | ||
"Parallel API needs -DWITH_CUDA on building libcgotorch.so"); | ||
#endif | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* Copyright 2020, GoTorch Authors */ | ||
#pragma once | ||
|
||
#include "cgotorch/torchdef.h" | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
//////////////////////////////////////////////////////////////////////////////// | ||
// Parallel | ||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
const char *DataParallel(char *go_module, void *f, Tensor input, Device *device, | ||
int64_t size, Device *output, int64_t dim); | ||
#ifdef __cplusplus | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package parallel | ||
|
||
// #cgo CFLAGS: -I ${SRCDIR}/../../ -I ${SRCDIR}../../cgotorch/libtorch/include | ||
// #cgo LDFLAGS: -L ${SRCDIR}/../../cgotorch -Wl,-rpath ${SRCDIR}/../../cgotorch -lcgotorch | ||
// #cgo LDFLAGS: -L ${SRCDIR}/../../cgotorch/libtorch/lib -Wl,-rpath ${SRCDIR}/../../cgotorch/libtorch/lib -lc10 -ltorch -ltorch_cpu | ||
// #include "cgotorch/cgotorch.h" | ||
// Tensor goModuleForward(char *m, Tensor input); | ||
import "C" | ||
import ( | ||
"reflect" | ||
"runtime" | ||
"unsafe" | ||
|
||
torch "github.com/wangkuiyi/gotorch" | ||
"github.com/wangkuiyi/gotorch/nn" | ||
) | ||
|
||
//export goModuleForward | ||
func goModuleForward(m *C.char, input C.Tensor) C.Tensor { | ||
module := (*(*nn.IModule)(unsafe.Pointer(m))) | ||
forward := reflect.ValueOf(module).MethodByName("Forward") | ||
args := []reflect.Value{reflect.ValueOf(torch.Tensor{(*unsafe.Pointer)(&input)})} | ||
return *(*C.Tensor)(forward.Call(args)[0].Interface().(torch.Tensor).T) | ||
} | ||
|
||
// DataParallel Evaluates module(input) in parallel across the given devices. | ||
// If `devices` is not supplied, the invocation is parallelized across all available CUDA devices. | ||
// If `outputDevice` is supplied, the final, combined tensor will be placed on this device. If not, it defaults to the first device in devices. | ||
// In detail, this method performs the following four distinct steps: | ||
// 1. Scatter the input to the given devices, | ||
// 2. Replicate (deep clone) the model on each device, | ||
// 3. Evaluate each module with its input on its device, | ||
// 4. Gather the outputs of each replica into a single output tensor, located on the `outputDevice`. | ||
func DataParallel(m nn.IModule, input torch.Tensor, devices []torch.Device, outputDevice torch.Device, dim int64) torch.Tensor { | ||
// Convert `m` to `*C.char` to workaround the "cgo argument has Go pointer to Go | ||
// pointer" check | ||
torch.MustNil(unsafe.Pointer(C.DataParallel((*C.char)(unsafe.Pointer(&m)), C.goModuleForward, *(*C.Tensor)(input.T), nil, 0, nil, 0))) | ||
runtime.KeepAlive(&m) | ||
runtime.KeepAlive(&input) | ||
return torch.Tensor{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package parallel | ||
|
||
import ( | ||
"fmt" | ||
"github.com/stretchr/testify/assert" | ||
torch "github.com/wangkuiyi/gotorch" | ||
"github.com/wangkuiyi/gotorch/nn" | ||
"testing" | ||
) | ||
|
||
type myModelModule struct { | ||
nn.Module // Every model must derive from Module | ||
} | ||
|
||
// Forward executes the calculation | ||
func (m *myModelModule) Forward(x torch.Tensor) torch.Tensor { | ||
fmt.Println("Forward") | ||
return torch.RandN([]int64{1, 1}, false) | ||
} | ||
|
||
func myModel() *myModelModule { | ||
m := &myModelModule{} | ||
m.Init(m) | ||
return m | ||
} | ||
|
||
func TestDataParallel(t *testing.T) { | ||
m := myModel() | ||
// panic: Parallel API needs -DWITH_CUDA on building libcgotorch.so | ||
assert.Panics(t, func() { | ||
DataParallel(m, torch.Tensor{nil}, []torch.Device{}, torch.Device{}, 0) | ||
}) | ||
// Only for CUDA | ||
// DataParallel(m, torch.RandN([]int64{1,1}, false), []torch.Device{}, torch.Device{}, 0) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are two approaches for data parallelism in for multi-GPU training:
PyTorch DistributedDataParallel has proved that Per Process Per GPU is more efficient.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, scatter--> parallel apply --> gather is not suggested. Instead, we launch a training process for each device. Each training process does dataloading/forward/backward/allreduce/update individually.