-
Notifications
You must be signed in to change notification settings - Fork 25.7k
[Pytoch][Vulkan] Create context for layernorm #114701
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
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/114701
Note: Links to docs will display an error until the docs builds have been completed. ✅ No FailuresAs of commit f96df3b with merge base e891a3b ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
This pull request was exported from Phabricator. Differential Revision: D51530478 |
aa81c68 to
6e7c759
Compare
Summary: `Layernorm` has two arguments weight and bias which are stored as constant tensors on the CPU and they are transferred to GPU at every inference call. We create a context for this op to avoid the repeated passing. Specifically, we - created `create_layernorm_context` and `run_layernorm_context` in `Layernorm.h` and `Layernorm.cpp` - registered them in `Register.cpp` - rewrote the graph representation of the op in `vulkan_rewrite.cpp` Test Plan: ## Numerical test ``` [luwei@devbig984.prn1 /data/users/luwei/fbsource (b6ccc956c)]$ LD_LIBRARY_PATH=third-party/swiftshader/lib/linux-x64/ buck run fbcode/mode/dev-nosan //xplat/caffe2:pt_vulkan_api_test_bin -- --gtest_filter="*layer_norm*" Recommended: For faster builds try buck2: replace 'buck' with 'buck2' NOTE: buck-out/ has changed: look for files in fbsource/buck-out/v2/ 'buck2 build --show-output //xplat/caffe2:pt_vulkan_api_test_bin' will print the new output paths. If you are building in fbsource//xplat and have questions, post in 'Cross Platform Dev Discussions': https://fb.workplace.com/groups/xplat.qa Targets matching .buckconfig buck2.supported_projects: {'//xplat/caffe2:pt_vulkan_api_test_bin': '//xplat'} To suppress this warning: touch ~/.config/.dont_hint_buck2 Building: finished in 0.1 sec (100%) 339/339 jobs, 0/339 updated Total time: 0.2 sec BUILD SUCCEEDED Running main() from third-party/googletest/1.14.0/googletest/googletest/src/gtest_main.cc Note: Google Test filter = *layer_norm* [==========] Running 10 tests from 1 test suite. [----------] Global test environment set-up. [----------] 10 tests from VulkanAPITest [ RUN ] VulkanAPITest.packed_layer_norm_2d [ OK ] VulkanAPITest.packed_layer_norm_2d (342 ms) [ RUN ] VulkanAPITest.packed_layer_norm_3d [ OK ] VulkanAPITest.packed_layer_norm_3d (284 ms) [ RUN ] VulkanAPITest.packed_layer_norm_4d [ OK ] VulkanAPITest.packed_layer_norm_4d (5 ms) [ RUN ] VulkanAPITest.layer_norm_invalid_inputs [ OK ] VulkanAPITest.layer_norm_invalid_inputs (28 ms) [ RUN ] VulkanAPITest.layer_norm_2d [ OK ] VulkanAPITest.layer_norm_2d (1 ms) [ RUN ] VulkanAPITest.layer_norm_3d [ OK ] VulkanAPITest.layer_norm_3d (2 ms) [ RUN ] VulkanAPITest.layer_norm_4d [ OK ] VulkanAPITest.layer_norm_4d (4 ms) [ RUN ] VulkanAPITest.native_layer_norm_2d [ OK ] VulkanAPITest.native_layer_norm_2d (1 ms) [ RUN ] VulkanAPITest.native_layer_norm_3d [ OK ] VulkanAPITest.native_layer_norm_3d (2 ms) [ RUN ] VulkanAPITest.native_layer_norm_4d [ OK ] VulkanAPITest.native_layer_norm_4d (6 ms) [----------] 10 tests from VulkanAPITest (679 ms total) [----------] Global test environment tear-down [==========] 10 tests from 1 test suite ran. (679 ms total) [ PASSED ] 10 tests. ``` Full test result in P888496077, summary as below ``` [----------] 419 tests from VulkanAPITest (21652 ms total) [----------] Global test environment tear-down [==========] 419 tests from 1 test suite ran. (21652 ms total) [ PASSED ] 418 tests. [ SKIPPED ] 1 test, listed below: [ SKIPPED ] VulkanAPITest.querypool_flushed_shader_log ``` ## Graph representation comparison We created a model using `layer_norm` and traced it as below ``` class MyModel(torch.nn.Module): def __init__(self): super(MyModel, self).__init__() self.layer_norm = torch.nn.LayerNorm(normalized_shape=10) def forward(self, x): return self.layer_norm(x) # Create an instance of the model model = MyModel() # Create a dummy input tensor for tracing input_tensor = torch.randn(1, 10) # Use torch.jit.trace to trace the model and generate a graph traced_model = torch.jit.trace(model, input_tensor) ``` Then we converted the traced model to Vulkan backend using `optimize_for_mobile` ``` from torch.utils import mobile_optimizer vulkan_model = mobile_optimizer.optimize_for_mobile( traced_model, backend="vulkan", preserved_methods=to_preserve ) ``` Then we can print the graph of the `vulkan_model` as `print(vk_model.graph)` - Before this diff ``` %4 : bool = prim::Constant[value=1](), scope: __module.layer_norm # /mnt/xarfuse/uid-602118/33e18f68-seed-nspid4026531836_cgpid32066351-ns-4026531840/torch/nn/functional.py:2546:0 %5 : float = prim::Constant[value=1.0000000000000001e-05](), scope: __module.layer_norm # /mnt/xarfuse/uid-602118/33e18f68-seed-nspid4026531836_cgpid32066351-ns-4026531840/torch/nn/functional.py:2546:0 %14 : int[] = prim::Constant[value=[10]]() %33 : Tensor = aten::to(%x, %53, %30, %31, %31) %10 : Tensor = aten::layer_norm(%33, %14, %self.layer_norm.weight, %self.layer_norm.bias, %5, %4), scope: __module.layer_norm # /mnt/xarfuse/uid-602118/33e18f68-seed-nspid4026531836_cgpid32066351-ns-4026531840/torch/nn/functional.py:2546:0 ``` - after this diff ``` %14 : int[] = prim::Constant[value=[10]]() %47 : Tensor = aten::to(%x, %78, %44, %45, %45) %16 : Tensor = vulkan_prepack::run_layernorm_context(%47, %14, %17) ``` Reviewed By: SS-JIA Differential Revision: D51530478
|
This pull request was exported from Phabricator. Differential Revision: D51530478 |
Summary: `Layernorm` has two arguments weight and bias which are stored as constant tensors on the CPU and they are transferred to GPU at every inference call. We create a context for this op to avoid the repeated passing. Specifically, we - created `create_layernorm_context` and `run_layernorm_context` in `Layernorm.h` and `Layernorm.cpp` - registered them in `Register.cpp` - rewrote the graph representation of the op in `vulkan_rewrite.cpp` Test Plan: ## Numerical test ``` [luwei@devbig984.prn1 /data/users/luwei/fbsource (b6ccc956c)]$ LD_LIBRARY_PATH=third-party/swiftshader/lib/linux-x64/ buck run fbcode/mode/dev-nosan //xplat/caffe2:pt_vulkan_api_test_bin -- --gtest_filter="*layer_norm*" Recommended: For faster builds try buck2: replace 'buck' with 'buck2' NOTE: buck-out/ has changed: look for files in fbsource/buck-out/v2/ 'buck2 build --show-output //xplat/caffe2:pt_vulkan_api_test_bin' will print the new output paths. If you are building in fbsource//xplat and have questions, post in 'Cross Platform Dev Discussions': https://fb.workplace.com/groups/xplat.qa Targets matching .buckconfig buck2.supported_projects: {'//xplat/caffe2:pt_vulkan_api_test_bin': '//xplat'} To suppress this warning: touch ~/.config/.dont_hint_buck2 Building: finished in 0.1 sec (100%) 339/339 jobs, 0/339 updated Total time: 0.2 sec BUILD SUCCEEDED Running main() from third-party/googletest/1.14.0/googletest/googletest/src/gtest_main.cc Note: Google Test filter = *layer_norm* [==========] Running 10 tests from 1 test suite. [----------] Global test environment set-up. [----------] 10 tests from VulkanAPITest [ RUN ] VulkanAPITest.packed_layer_norm_2d [ OK ] VulkanAPITest.packed_layer_norm_2d (342 ms) [ RUN ] VulkanAPITest.packed_layer_norm_3d [ OK ] VulkanAPITest.packed_layer_norm_3d (284 ms) [ RUN ] VulkanAPITest.packed_layer_norm_4d [ OK ] VulkanAPITest.packed_layer_norm_4d (5 ms) [ RUN ] VulkanAPITest.layer_norm_invalid_inputs [ OK ] VulkanAPITest.layer_norm_invalid_inputs (28 ms) [ RUN ] VulkanAPITest.layer_norm_2d [ OK ] VulkanAPITest.layer_norm_2d (1 ms) [ RUN ] VulkanAPITest.layer_norm_3d [ OK ] VulkanAPITest.layer_norm_3d (2 ms) [ RUN ] VulkanAPITest.layer_norm_4d [ OK ] VulkanAPITest.layer_norm_4d (4 ms) [ RUN ] VulkanAPITest.native_layer_norm_2d [ OK ] VulkanAPITest.native_layer_norm_2d (1 ms) [ RUN ] VulkanAPITest.native_layer_norm_3d [ OK ] VulkanAPITest.native_layer_norm_3d (2 ms) [ RUN ] VulkanAPITest.native_layer_norm_4d [ OK ] VulkanAPITest.native_layer_norm_4d (6 ms) [----------] 10 tests from VulkanAPITest (679 ms total) [----------] Global test environment tear-down [==========] 10 tests from 1 test suite ran. (679 ms total) [ PASSED ] 10 tests. ``` Full test result in P888496077, summary as below ``` [----------] 419 tests from VulkanAPITest (21652 ms total) [----------] Global test environment tear-down [==========] 419 tests from 1 test suite ran. (21652 ms total) [ PASSED ] 418 tests. [ SKIPPED ] 1 test, listed below: [ SKIPPED ] VulkanAPITest.querypool_flushed_shader_log ``` ## Graph representation comparison We created a model using `layer_norm` and traced it as below ``` class MyModel(torch.nn.Module): def __init__(self): super(MyModel, self).__init__() self.layer_norm = torch.nn.LayerNorm(normalized_shape=10) def forward(self, x): return self.layer_norm(x) # Create an instance of the model model = MyModel() # Create a dummy input tensor for tracing input_tensor = torch.randn(1, 10) # Use torch.jit.trace to trace the model and generate a graph traced_model = torch.jit.trace(model, input_tensor) ``` Then we converted the traced model to Vulkan backend using `optimize_for_mobile` ``` from torch.utils import mobile_optimizer vulkan_model = mobile_optimizer.optimize_for_mobile( traced_model, backend="vulkan", preserved_methods=to_preserve ) ``` Then we can print the graph of the `vulkan_model` as `print(vk_model.graph)` - Before this diff ``` %4 : bool = prim::Constant[value=1](), scope: __module.layer_norm # /mnt/xarfuse/uid-602118/33e18f68-seed-nspid4026531836_cgpid32066351-ns-4026531840/torch/nn/functional.py:2546:0 %5 : float = prim::Constant[value=1.0000000000000001e-05](), scope: __module.layer_norm # /mnt/xarfuse/uid-602118/33e18f68-seed-nspid4026531836_cgpid32066351-ns-4026531840/torch/nn/functional.py:2546:0 %14 : int[] = prim::Constant[value=[10]]() %33 : Tensor = aten::to(%x, %53, %30, %31, %31) %10 : Tensor = aten::layer_norm(%33, %14, %self.layer_norm.weight, %self.layer_norm.bias, %5, %4), scope: __module.layer_norm # /mnt/xarfuse/uid-602118/33e18f68-seed-nspid4026531836_cgpid32066351-ns-4026531840/torch/nn/functional.py:2546:0 ``` - after this diff ``` %14 : int[] = prim::Constant[value=[10]]() %47 : Tensor = aten::to(%x, %78, %44, %45, %45) %16 : Tensor = vulkan_prepack::run_layernorm_context(%47, %14, %17) ``` Reviewed By: SS-JIA Differential Revision: D51530478
6e7c759 to
f96df3b
Compare
|
This pull request was exported from Phabricator. Differential Revision: D51530478 |
|
@pytorchbot merge (Initiating merge automatically since Phabricator Diff has merged) |
Merge startedYour change will be merged once all checks pass (ETA 0-4 Hours). Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
Summary: `Layernorm` has two arguments weight and bias which are stored as constant tensors on the CPU and they are transferred to GPU at every inference call. We create a context for this op to avoid the repeated passing. Specifically, we - created `create_layernorm_context` and `run_layernorm_context` in `Layernorm.h` and `Layernorm.cpp` - registered them in `Register.cpp` - rewrote the graph representation of the op in `vulkan_rewrite.cpp` Test Plan: ## Numerical test ``` [luwei@devbig984.prn1 /data/users/luwei/fbsource (b6ccc956c)]$ LD_LIBRARY_PATH=third-party/swiftshader/lib/linux-x64/ buck run fbcode/mode/dev-nosan //xplat/caffe2:pt_vulkan_api_test_bin -- --gtest_filter="*layer_norm*" Recommended: For faster builds try buck2: replace 'buck' with 'buck2' NOTE: buck-out/ has changed: look for files in fbsource/buck-out/v2/ 'buck2 build --show-output //xplat/caffe2:pt_vulkan_api_test_bin' will print the new output paths. If you are building in fbsource//xplat and have questions, post in 'Cross Platform Dev Discussions': https://fb.workplace.com/groups/xplat.qa Targets matching .buckconfig buck2.supported_projects: {'//xplat/caffe2:pt_vulkan_api_test_bin': '//xplat'} To suppress this warning: touch ~/.config/.dont_hint_buck2 Building: finished in 0.1 sec (100%) 339/339 jobs, 0/339 updated Total time: 0.2 sec BUILD SUCCEEDED Running main() from third-party/googletest/1.14.0/googletest/googletest/src/gtest_main.cc Note: Google Test filter = *layer_norm* [==========] Running 10 tests from 1 test suite. [----------] Global test environment set-up. [----------] 10 tests from VulkanAPITest [ RUN ] VulkanAPITest.packed_layer_norm_2d [ OK ] VulkanAPITest.packed_layer_norm_2d (342 ms) [ RUN ] VulkanAPITest.packed_layer_norm_3d [ OK ] VulkanAPITest.packed_layer_norm_3d (284 ms) [ RUN ] VulkanAPITest.packed_layer_norm_4d [ OK ] VulkanAPITest.packed_layer_norm_4d (5 ms) [ RUN ] VulkanAPITest.layer_norm_invalid_inputs [ OK ] VulkanAPITest.layer_norm_invalid_inputs (28 ms) [ RUN ] VulkanAPITest.layer_norm_2d [ OK ] VulkanAPITest.layer_norm_2d (1 ms) [ RUN ] VulkanAPITest.layer_norm_3d [ OK ] VulkanAPITest.layer_norm_3d (2 ms) [ RUN ] VulkanAPITest.layer_norm_4d [ OK ] VulkanAPITest.layer_norm_4d (4 ms) [ RUN ] VulkanAPITest.native_layer_norm_2d [ OK ] VulkanAPITest.native_layer_norm_2d (1 ms) [ RUN ] VulkanAPITest.native_layer_norm_3d [ OK ] VulkanAPITest.native_layer_norm_3d (2 ms) [ RUN ] VulkanAPITest.native_layer_norm_4d [ OK ] VulkanAPITest.native_layer_norm_4d (6 ms) [----------] 10 tests from VulkanAPITest (679 ms total) [----------] Global test environment tear-down [==========] 10 tests from 1 test suite ran. (679 ms total) [ PASSED ] 10 tests. ``` Full test result in P888496077, summary as below ``` [----------] 419 tests from VulkanAPITest (21652 ms total) [----------] Global test environment tear-down [==========] 419 tests from 1 test suite ran. (21652 ms total) [ PASSED ] 418 tests. [ SKIPPED ] 1 test, listed below: [ SKIPPED ] VulkanAPITest.querypool_flushed_shader_log ``` ## Graph representation comparison We created a model using `layer_norm` and traced it as below ``` class MyModel(torch.nn.Module): def __init__(self): super(MyModel, self).__init__() self.layer_norm = torch.nn.LayerNorm(normalized_shape=10) def forward(self, x): return self.layer_norm(x) # Create an instance of the model model = MyModel() # Create a dummy input tensor for tracing input_tensor = torch.randn(1, 10) # Use torch.jit.trace to trace the model and generate a graph traced_model = torch.jit.trace(model, input_tensor) ``` Then we converted the traced model to Vulkan backend using `optimize_for_mobile` ``` from torch.utils import mobile_optimizer vulkan_model = mobile_optimizer.optimize_for_mobile( traced_model, backend="vulkan", preserved_methods=to_preserve ) ``` Then we can print the graph of the `vulkan_model` as `print(vk_model.graph)` - Before this diff ``` %4 : bool = prim::Constant[value=1](), scope: __module.layer_norm # /mnt/xarfuse/uid-602118/33e18f68-seed-nspid4026531836_cgpid32066351-ns-4026531840/torch/nn/functional.py:2546:0 %5 : float = prim::Constant[value=1.0000000000000001e-05](), scope: __module.layer_norm # /mnt/xarfuse/uid-602118/33e18f68-seed-nspid4026531836_cgpid32066351-ns-4026531840/torch/nn/functional.py:2546:0 %14 : int[] = prim::Constant[value=[10]]() %33 : Tensor = aten::to(%x, %53, %30, %31, %31) %10 : Tensor = aten::layer_norm(%33, %14, %self.layer_norm.weight, %self.layer_norm.bias, %5, %4), scope: __module.layer_norm # /mnt/xarfuse/uid-602118/33e18f68-seed-nspid4026531836_cgpid32066351-ns-4026531840/torch/nn/functional.py:2546:0 ``` - after this diff ``` %14 : int[] = prim::Constant[value=[10]]() %47 : Tensor = aten::to(%x, %78, %44, %45, %45) %16 : Tensor = vulkan_prepack::run_layernorm_context(%47, %14, %17) ``` Reviewed By: SS-JIA Differential Revision: D51530478 Pull Request resolved: pytorch#114701 Approved by: https://github.com/yipjustin
Summary:
Layernormhas two arguments weight and bias which are stored as constant tensors on the CPU and they are transferred to GPU at every inference call. We create a context for this op to avoid the repeated passing. Specifically, wecreate_layernorm_contextandrun_layernorm_contextinLayernorm.handLayernorm.cppRegister.cppvulkan_rewrite.cppTest Plan:
Numerical test
Full test result in P888496077, summary as below
Graph representation comparison
We created a model using
layer_normand traced it as belowThen we converted the traced model to Vulkan backend using
optimize_for_mobileThen we can print the graph of the
vulkan_modelasprint(vk_model.graph)Reviewed By: SS-JIA
Differential Revision: D51530478