in-place copy from src to dst src and dst should be the same layer type, e.g., both are LayerNorm or both are Linear. Or at least, both have same named_parameters name and shape.
(src, dst)
| 8 | model = ViTModel(args) |
| 9 | |
| 10 | def copy_layer_param(src, dst): |
| 11 | """ |
| 12 | in-place copy from src to dst |
| 13 | src and dst should be the same layer type, e.g., both are LayerNorm or both are Linear. |
| 14 | Or at least, both have same named_parameters name and shape. |
| 15 | """ |
| 16 | src_dic = dict(src.named_parameters()) |
| 17 | dst_dic = dict(dst.named_parameters()) |
| 18 | for k in dst_dic: |
| 19 | assert dst_dic[k].data.shape == src_dic[k].data.shape |
| 20 | dst_dic[k].data = src_dic[k].data |
| 21 | assert (dst_dic[k].data == src_dic[k].data).all() |
| 22 | |
| 23 | def copy_from_param(src, dst): |
| 24 | assert src.data.shape == dst.data.shape |
no outgoing calls
no test coverage detected