Skip to content

[JIT] AutoBatching - IR transformation(basic operators)#9198

Closed
ChunliF wants to merge 21 commits intopytorch:masterfrom
ChunliF:batchir
Closed

[JIT] AutoBatching - IR transformation(basic operators)#9198
ChunliF wants to merge 21 commits intopytorch:masterfrom
ChunliF:batchir

Conversation

@ChunliF
Copy link
Contributor

@ChunliF ChunliF commented Jul 6, 2018

Use decorator torch.jit.batch to implement auto-batching (call to_batch pass to do IR tranformation).

  • to_batch pass: "to_batch.h/cpp" in csrc/jit/passess to transform a graph to a new batched graph.
  • Write several basic operators for BatchTensor (add, mul, sigmoid, tanh, mm, matmul, select).
  • Register the operators in a lookup table <std::string, std::shared_ptr<Graph>>. (use the Graph to replace the original node in IR graph)

Move BatchTensor in python from torch.BatchTensor to torch.jit.BatchTensor

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ChunliF has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

Copy link
Collaborator

@jamesr66a jamesr66a left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good!

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ChunliF has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ChunliF has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ChunliF has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

Copy link
Collaborator

@jamesr66a jamesr66a left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@fmassa fmassa mentioned this pull request Jul 10, 2018
Copy link
Contributor

@zdevito zdevito left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks pretty good! I mostly have code organizational comments and a few nits.

}

void to_batch_graph(std::shared_ptr<Graph>& graph, int64_t batch_size){
// batch_size: not used yet, will be used to deal with scalarType

This comment was marked as off-topic.

This comment was marked as off-topic.

namespace torch { namespace jit {

// map from batchTensor to {data, mask, dims}
static std::unordered_map<Value*, std::vector<Value*>> batch_map;

This comment was marked as off-topic.

auto new_input = batch_map.at(input);
new_inputs.insert(new_inputs.end(), new_input.begin(), new_input.end());
}
else{

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

batch_map[output] = std::vector<Value*>(outputs.begin() + i * 3, outputs.begin() + i * 3 + 3);
}
}
// control flow: not supported yet, will be added further

This comment was marked as off-topic.

// There is a thin wrapper on top of this method in the C++ version of
// ScriptModule.
return runMethodFromPython(self.get_method("forward"), args);
})

This comment was marked as off-topic.

This comment was marked as off-topic.

def batch(batch_size=1, optimize=True, _frames_up=0):
def decorator(fn):
mod = script(fn, optimize, _frames_up)
res_graph = mod.to_batch(batch_size)

This comment was marked as off-topic.

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ChunliF has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ChunliF has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ChunliF is landing this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@ChunliF ChunliF deleted the batchir branch July 12, 2018 17:42
goodlux pushed a commit to goodlux/pytorch that referenced this pull request Aug 15, 2018
Summary:
Use decorator `torch.jit.batch` to implement auto-batching (call `to_batch` pass to do IR tranformation).
- `to_batch` pass: "to_batch.h/cpp" in csrc/jit/passess to transform a graph to a new batched graph.
- Write several basic operators for BatchTensor (add, mul, sigmoid, tanh, mm, matmul, select).
- Register the operators in a lookup table `<std::string, std::shared_ptr<Graph>>`. (use the Graph to replace the original node in IR graph)

Move BatchTensor in python from torch.BatchTensor to torch.jit.BatchTensor
Pull Request resolved: pytorch#9198

Reviewed By: zdevito

Differential Revision: D8744466

Pulled By: ChunliF

fbshipit-source-id: 9ea56a30f55cb870f13a2069a47cc635419763ff
@ezyang ezyang added the merged label Jun 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants