-
Notifications
You must be signed in to change notification settings - Fork 689
Torchscript support #159
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
Merged
Merged
Torchscript support #159
Changes from all commits
Commits
Show all changes
44 commits
Select commit
Hold shift + click to select a range
2bd3cdb
support torchscript
grimoire 7f06599
add nms
grimoire 87cc4a0
add torchscript configs and update deploy process and dump-info
AllentDan 310f28b
Merge branch 'torchscript-support-my' into torchscript-support
AllentDan 22b46d7
typescript -> torchscript
AllentDan 96909ec
add torchscript custom extension support
9a26bfc
add ts custom ops again
2b6f5fa
support mmseg unet
AllentDan 496751c
[WIP] add optimizer for torchscript (#119)
5ae4609
Torchscript optimizer python api (#121)
a25f360
Merge Master, update optimizer (#151)
dc2472d
configs for all tasks
AllentDan fb5f752
use torchvision roi align
AllentDan 71c4786
resolve conflicts
AllentDan c69a957
remote unnecessary code
024a328
fix ut
AllentDan c4c8a4d
fix ut
AllentDan f9b914e
export
AllentDan d6c9b81
det dynamic
AllentDan afd4918
det dynamic
AllentDan d3ee3ef
add ut
AllentDan 98f3450
fix ut
AllentDan a2d22da
add ut and docs
AllentDan b1b0e8c
fix ut
AllentDan c742ba0
skip torchscript ut if no ops available
AllentDan 2ffddde
add torchscript option to build.md
AllentDan ffe0630
update benchmark and resolve comments
AllentDan 04e6dd9
resolve conflicts
AllentDan 5bf0411
resolve conflicts
AllentDan 9189cf9
rename configs
AllentDan 82c5a41
fix mrcnn cuda test
AllentDan 817d4a0
remove useless
AllentDan 6f74cdc
add version requirements to docs and comments to codes
AllentDan fb2dcce
resolve conflicts
AllentDan 796fb67
enable empty image exporting for torchscript and accelerate ORT infer…
AllentDan bc428b4
Merge branch 'dev-v0.4.0' into torchscript-support
AllentDan 2a4f23c
rebase
AllentDan 616d86e
update example for torchscript.md
AllentDan d3dc541
update FAQs for torchscript.md
AllentDan a456c55
resolve comments
AllentDan 2f79231
only use torchvision roi_align for torchscript
AllentDan 1471241
fix ut
AllentDan 25f9693
use torchvision roi align when pool model is avg
AllentDan 8ed6b86
resolve comments
AllentDan 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 hidden or 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 @@ | ||
| backend_config = dict(type='torchscript') |
This file contains hidden or 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,6 @@ | ||
| ir_config = dict( | ||
| type='torchscript', | ||
| save_file='end2end.pt', | ||
| input_names=['input'], | ||
| output_names=['output'], | ||
| input_shape=None) |
This file contains hidden or 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,6 @@ | ||
| _base_ = [ | ||
| '../_base_/torchscript_config.py', '../_base_/backends/torchscript.py' | ||
| ] | ||
|
|
||
| ir_config = dict(input_shape=None) | ||
| codebase_config = dict(type='mmcls', task='Classification') |
This file contains hidden or 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,4 @@ | ||
| _base_ = ['./base_torchscript.py'] | ||
|
|
||
| ir_config = dict(output_names=['dets', 'labels', 'masks']) | ||
| codebase_config = dict(post_processing=dict(export_postprocess_mask=False)) |
This file contains hidden or 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,16 @@ | ||
| _base_ = ['../../_base_/torchscript_config.py'] | ||
|
|
||
| ir_config = dict(output_names=['dets', 'labels']) | ||
| codebase_config = dict( | ||
| type='mmdet', | ||
| task='ObjectDetection', | ||
| model_type='end2end', | ||
| post_processing=dict( | ||
| score_threshold=0.05, | ||
| confidence_threshold=0.005, # for YOLOv3 | ||
| iou_threshold=0.5, | ||
| max_output_boxes_per_class=200, | ||
| pre_top_k=5000, | ||
| keep_top_k=100, | ||
| background_label_id=-1, | ||
| )) |
This file contains hidden or 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,3 @@ | ||
| _base_ = [ | ||
| '../_base_/base_torchscript.py', '../../_base_/backends/torchscript.py' | ||
| ] |
This file contains hidden or 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,4 @@ | ||
| _base_ = [ | ||
| '../_base_/base_instance-seg_torchscript.py', | ||
| '../../_base_/backends/torchscript.py' | ||
| ] | ||
7 changes: 7 additions & 0 deletions
7
configs/mmedit/super-resolution/super-resolution_torchscript.py
This file contains hidden or 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,7 @@ | ||
| _base_ = [ | ||
| '../../_base_/torchscript_config.py', | ||
| '../../_base_/backends/torchscript.py' | ||
| ] | ||
|
|
||
| ir_config = dict(input_shape=None) | ||
| codebase_config = dict(type='mmedit', task='SuperResolution') |
This file contains hidden or 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,7 @@ | ||
| _base_ = [ | ||
| '../../_base_/torchscript_config.py', | ||
| '../../_base_/backends/torchscript.py' | ||
| ] | ||
|
|
||
| ir_config = dict(input_shape=None) | ||
| codebase_config = dict(type='mmocr', task='TextDetection') |
7 changes: 7 additions & 0 deletions
7
configs/mmocr/text-recognition/text-recognition_torchscript.py
This file contains hidden or 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,7 @@ | ||
| _base_ = [ | ||
| '../../_base_/torchscript_config.py', | ||
| '../../_base_/backends/torchscript.py' | ||
| ] | ||
|
|
||
| ir_config = dict(input_shape=None) | ||
| codebase_config = dict(type='mmocr', task='TextRecognition') |
This file contains hidden or 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,6 @@ | ||
| _base_ = [ | ||
| '../_base_/torchscript_config.py', '../_base_/backends/torchscript.py' | ||
| ] | ||
|
|
||
| ir_config = dict(input_shape=None) | ||
| codebase_config = dict(type='mmseg', task='Segmentation') |
This file contains hidden or 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
94 changes: 94 additions & 0 deletions
94
csrc/backend_ops/common/modulated_deform_conv/common_cuda_helper.cuh
This file contains hidden or 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,94 @@ | ||
| // Copyright (c) OpenMMLab. All rights reserved. | ||
| #ifndef COMMON_CUDA_HELPER | ||
| #define COMMON_CUDA_HELPER | ||
|
|
||
| #include <cublas_v2.h> | ||
| #include <cuda.h> | ||
|
|
||
| #include <algorithm> | ||
|
|
||
| #define CUDA_1D_KERNEL_LOOP(i, n) \ | ||
| for (int i = blockIdx.x * blockDim.x + threadIdx.x; i < (n); i += blockDim.x * gridDim.x) | ||
|
|
||
| #define THREADS_PER_BLOCK 512 | ||
|
|
||
| #define DIVUP(m, n) ((m) / (n) + ((m) % (n) > 0)) | ||
| inline int GET_BLOCKS(const int N) { | ||
| int optimal_block_num = DIVUP(N, THREADS_PER_BLOCK); | ||
| int max_block_num = 4096; | ||
| return std::min(optimal_block_num, max_block_num); | ||
| } | ||
|
|
||
| #define cudaCheckError() \ | ||
| { \ | ||
| cudaError_t e = cudaGetLastError(); \ | ||
| if (e != cudaSuccess) { \ | ||
| printf("Cuda failure %s:%d: '%s'\n", __FILE__, __LINE__, cudaGetErrorString(e)); \ | ||
| exit(0); \ | ||
| } \ | ||
| } | ||
|
|
||
| /** | ||
| * Returns a view of the original tensor with its dimensions permuted. | ||
| * | ||
| * @param[out] dst pointer to the destination tensor | ||
| * @param[in] src pointer to the source tensor | ||
| * @param[in] src_size shape of the src tensor | ||
| * @param[in] permute The desired ordering of dimensions | ||
| * @param[in] src_dim dim of src tensor | ||
| * @param[in] stream cuda stream handle | ||
| */ | ||
| template <class scalar_t> | ||
| void memcpyPermute(scalar_t* dst, const scalar_t* src, int* src_size, int* permute, int src_dim, | ||
| cudaStream_t stream = 0); | ||
|
|
||
| template <typename scalar_t> | ||
| cublasStatus_t cublasGemmWrap(cublasHandle_t handle, cublasOperation_t transa, | ||
| cublasOperation_t transb, int m, int n, int k, const scalar_t* alpha, | ||
| const scalar_t* A, int lda, const scalar_t* B, int ldb, | ||
| const scalar_t* beta, scalar_t* C, int ldc); | ||
|
|
||
| template <typename scalar_t> | ||
| __device__ scalar_t bilinear_interpolate(const scalar_t* input, const int height, const int width, | ||
| scalar_t y, scalar_t x) { | ||
| // deal with cases that inverse elements are out of feature map boundary | ||
| if (y < -1.0 || y > height || x < -1.0 || x > width) return 0; | ||
|
|
||
| if (y <= 0) y = 0; | ||
| if (x <= 0) x = 0; | ||
|
|
||
| int y_low = (int)y; | ||
| int x_low = (int)x; | ||
| int y_high; | ||
| int x_high; | ||
|
|
||
| if (y_low >= height - 1) { | ||
| y_high = y_low = height - 1; | ||
| y = (scalar_t)y_low; | ||
| } else { | ||
| y_high = y_low + 1; | ||
| } | ||
|
|
||
| if (x_low >= width - 1) { | ||
| x_high = x_low = width - 1; | ||
| x = (scalar_t)x_low; | ||
| } else { | ||
| x_high = x_low + 1; | ||
| } | ||
|
|
||
| scalar_t ly = y - y_low; | ||
| scalar_t lx = x - x_low; | ||
| scalar_t hy = 1. - ly, hx = 1. - lx; | ||
| // do bilinear interpolation | ||
| scalar_t v1 = input[y_low * width + x_low]; | ||
| scalar_t v2 = input[y_low * width + x_high]; | ||
| scalar_t v3 = input[y_high * width + x_low]; | ||
| scalar_t v4 = input[y_high * width + x_high]; | ||
| scalar_t w1 = hy * hx, w2 = hy * lx, w3 = ly * hx, w4 = ly * lx; | ||
|
|
||
| scalar_t val = (w1 * v1 + w2 * v2 + w3 * v3 + w4 * v4); | ||
|
|
||
| return val; | ||
| } | ||
|
|
||
| #endif // COMMON_CUDA_HELPER |
82 changes: 82 additions & 0 deletions
82
csrc/backend_ops/common/modulated_deform_conv/modulated_deform_conv_cpu.h
This file contains hidden or 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,82 @@ | ||
| #include <cmath> | ||
| #include <cstdint> | ||
|
|
||
| template <typename T> | ||
| T bilinear_interpolate_2d(const T *src, const int64_t src_h, const int64_t src_w, const T h, | ||
| const T w) { | ||
| if (h <= -1 || src_h <= h || w <= -1 || src_w <= w) { | ||
| return 0; | ||
| } | ||
|
|
||
| int64_t h_low = floor(h); | ||
| int64_t w_low = floor(w); | ||
| int64_t h_high = h_low + 1; | ||
| int64_t w_high = w_low + 1; | ||
|
|
||
| T lh = h - h_low; | ||
| T lw = w - w_low; | ||
| T hh = 1 - lh; | ||
| T hw = 1 - lw; | ||
|
|
||
| T v1 = 0; | ||
| if (h_low >= 0 && w_low >= 0) v1 = src[h_low * src_w + w_low]; | ||
| T v2 = 0; | ||
| if (h_low >= 0 && w_high <= src_w - 1) v2 = src[h_low * src_w + w_high]; | ||
| T v3 = 0; | ||
| if (h_high <= src_h - 1 && w_low >= 0) v3 = src[h_high * src_w + w_low]; | ||
| T v4 = 0; | ||
| if (h_high <= src_h - 1 && w_high <= src_w - 1) v4 = src[h_high * src_w + w_high]; | ||
|
|
||
| T w1 = hh * hw, w2 = hh * lw, w3 = lh * hw, w4 = lh * lw; | ||
|
|
||
| T val = (w1 * v1 + w2 * v2 + w3 * v3 + w4 * v4); | ||
| return val; | ||
| } | ||
|
|
||
| // output: (channels * kernel_h * kernel_w, dst_h * dst_w) | ||
| template <typename T> | ||
| void deformable_im2col_2d(const T *input, const T *offset, const T *mask, const int64_t src_h, | ||
| const int64_t src_w, const int64_t kernel_h, const int64_t kernel_w, | ||
| const int64_t pad_h, const int64_t pad_w, const int64_t stride_h, | ||
| const int64_t stride_w, const int64_t dilation_h, | ||
| const int64_t dilation_w, const int64_t channels, | ||
| const int64_t offset_groups, const int64_t dst_h, const int64_t dst_w, | ||
| const bool use_mask, T *columns) { | ||
| const int64_t workload = channels * dst_h * dst_w; | ||
| for (int64_t index = 0; index != workload; ++index) { | ||
| const int64_t ow = index % dst_w; | ||
| const int64_t oh = (index / dst_w) % dst_h; | ||
| const int64_t ic = index / (dst_w * dst_h); | ||
| const int64_t oc = ic * kernel_h * kernel_w; | ||
|
|
||
| int64_t c_per_offset_grp = channels / offset_groups; | ||
| const int64_t grp_idx = ic / c_per_offset_grp; | ||
|
|
||
| auto columns_ptr = columns + (oc * (dst_h * dst_w) + oh * dst_w + ow); | ||
| auto input_ptr = input + ic * (src_h * src_w); | ||
| auto offset_ptr = offset + grp_idx * 2 * kernel_h * kernel_w * dst_h * dst_w; | ||
| auto mask_ptr = mask; | ||
| if (use_mask) { | ||
| mask_ptr += grp_idx * kernel_h * kernel_w * dst_h * dst_w; | ||
| } | ||
|
|
||
| for (int64_t kh = 0; kh < kernel_h; ++kh) { | ||
| for (int64_t kw = 0; kw < kernel_w; ++kw) { | ||
| const int64_t mask_idx = kh * kernel_w + kw; | ||
| const int64_t offset_idx = 2 * mask_idx; | ||
|
|
||
| T mask_value = 1; | ||
| if (use_mask) { | ||
| mask_value = mask_ptr[mask_idx * (dst_h * dst_w) + oh * dst_w + ow]; | ||
| } | ||
|
|
||
| const T offset_h = offset_ptr[offset_idx * (dst_h * dst_w) + oh * dst_w + ow]; | ||
| const T offset_w = offset_ptr[(offset_idx + 1) * (dst_h * dst_w) + oh * dst_w + ow]; | ||
| const T ih = (oh * stride_h - pad_h) + kh * dilation_h + offset_h; | ||
| const T iw = (ow * stride_w - pad_w) + kw * dilation_w + offset_w; | ||
| *columns_ptr = mask_value * bilinear_interpolate_2d<T>(input_ptr, src_h, src_w, ih, iw); | ||
| columns_ptr += dst_h * dst_w; | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.