|
| 1 | +# coding=utf-8 |
| 2 | +# Copyright 2021 The OneFlow Authors. All rights reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | + |
| 17 | +import os |
| 18 | + |
| 19 | +import oneflow as flow |
| 20 | +from oneflow import nn |
| 21 | + |
| 22 | +from libai.utils import distributed as dist |
| 23 | + |
| 24 | + |
| 25 | +class Conv1D(nn.Module): |
| 26 | + def __init__( |
| 27 | + self, |
| 28 | + in_features, |
| 29 | + out_features, |
| 30 | + bias=True, |
| 31 | + parallel="data", |
| 32 | + init_method=nn.init.xavier_normal_, |
| 33 | + skip_bias_add=False, |
| 34 | + dtype=flow.float32, |
| 35 | + *, |
| 36 | + layer_idx=0, |
| 37 | + ): |
| 38 | + super().__init__() |
| 39 | + self.in_features = in_features |
| 40 | + self.out_features = out_features |
| 41 | + self.parallel = parallel |
| 42 | + self.skip_bias_add = skip_bias_add |
| 43 | + |
| 44 | + if parallel == "col": |
| 45 | + weight_sbp = dist.get_nd_sbp([flow.sbp.broadcast, flow.sbp.split(1)]) |
| 46 | + bias_sbp = dist.get_nd_sbp([flow.sbp.broadcast, flow.sbp.broadcast]) |
| 47 | + |
| 48 | + elif parallel == "row": |
| 49 | + weight_sbp = dist.get_nd_sbp([flow.sbp.broadcast, flow.sbp.split(0)]) |
| 50 | + bias_sbp = dist.get_nd_sbp([flow.sbp.broadcast, flow.sbp.split(0)]) |
| 51 | + |
| 52 | + elif parallel == "data": |
| 53 | + weight_sbp = dist.get_nd_sbp([flow.sbp.broadcast, flow.sbp.broadcast]) |
| 54 | + bias_sbp = dist.get_nd_sbp([flow.sbp.broadcast, flow.sbp.broadcast]) |
| 55 | + |
| 56 | + else: |
| 57 | + raise KeyError(f"{parallel} is not supported! Only support ('data', 'row' and 'col')") |
| 58 | + |
| 59 | + self.weight = flow.nn.Parameter( |
| 60 | + flow.empty( |
| 61 | + (in_features, out_features), |
| 62 | + dtype=dtype, |
| 63 | + placement=dist.get_layer_placement(layer_idx), # for pipeline parallelism placement |
| 64 | + sbp=weight_sbp, |
| 65 | + ) |
| 66 | + ) |
| 67 | + if os.getenv("ONEFLOW_LINEAR_EMBEDDING_SKIP_INIT", "0") != "1": |
| 68 | + init_method(self.weight) |
| 69 | + |
| 70 | + self.bias = ( |
| 71 | + flow.nn.Parameter( |
| 72 | + flow.zeros( |
| 73 | + (out_features,), |
| 74 | + dtype=dtype, |
| 75 | + placement=dist.get_layer_placement(layer_idx), |
| 76 | + sbp=bias_sbp, |
| 77 | + ) |
| 78 | + ) |
| 79 | + if bias |
| 80 | + else None |
| 81 | + ) |
| 82 | + |
| 83 | + def forward(self, x): |
| 84 | + if dist.same_sbp(self.weight.sbp, dist.get_nd_sbp([flow.sbp.broadcast, flow.sbp.split(1)])): |
| 85 | + if self.weight.sbp[-1] == flow.sbp.split(1): |
| 86 | + x_sbp = x.sbp[:-1] + (flow.sbp.broadcast,) |
| 87 | + x = x.to_global(sbp=x_sbp) |
| 88 | + |
| 89 | + x = x.to_global(grad_sbp=x.sbp) |
| 90 | + x = flow.matmul(x, self.weight) |
| 91 | + |
| 92 | + elif dist.same_sbp( |
| 93 | + self.weight.sbp, dist.get_nd_sbp([flow.sbp.broadcast, flow.sbp.split(0)]) |
| 94 | + ): |
| 95 | + if self.weight.sbp[-1] == flow.sbp.split(0): |
| 96 | + x_sbp = x.sbp[:-1] + (flow.sbp.split(x.ndim - 1),) |
| 97 | + x = x.to_global(sbp=x_sbp) |
| 98 | + out_sbp = x.sbp[:-1] + (flow.sbp.broadcast,) |
| 99 | + else: |
| 100 | + out_sbp = x.sbp |
| 101 | + |
| 102 | + x = flow.matmul(x, self.weight) |
| 103 | + x = x.to_global(sbp=out_sbp) |
| 104 | + |
| 105 | + elif dist.same_sbp( |
| 106 | + self.weight.sbp, dist.get_nd_sbp([flow.sbp.broadcast, flow.sbp.broadcast]) |
| 107 | + ): |
| 108 | + x = x.to_global(grad_sbp=x.sbp) |
| 109 | + x = flow.matmul(x, self.weight) |
| 110 | + else: |
| 111 | + x = flow.matmul(x, self.weight) |
| 112 | + |
| 113 | + if self.bias is not None: |
| 114 | + if self.skip_bias_add: |
| 115 | + return x, self.bias |
| 116 | + else: |
| 117 | + return x + self.bias |
| 118 | + else: |
| 119 | + return x |
| 120 | + |
| 121 | + def extra_repr(self) -> str: |
| 122 | + return "in_features={}, out_features={}, bias={}, parallel={}".format( |
| 123 | + self.in_features, |
| 124 | + self.out_features, |
| 125 | + self.bias is not None, |
| 126 | + self.parallel, |
| 127 | + ) |
0 commit comments