diff --git a/examples/singa_peft/model/mlp.py b/examples/singa_peft/model/mlp.py new file mode 100644 index 000000000..9ae4e7e26 --- /dev/null +++ b/examples/singa_peft/model/mlp.py @@ -0,0 +1,85 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +import numpy as np +from singa import model +from singa import tensor +from singa import layer + + +np_dtype = {"float16": np.float16, "float32": np.float32} +singa_dtype = {"float16": tensor.float16, "float32": tensor.float32} + + +class MLP(model.Model): + def __init__(self, in_features=10, perceptron_size=100, num_classes=10): + super(MLP, self).__init__() + self.dimension = 2 + self.in_features = in_features + self.perceptron_size = perceptron_size + self.num_classes = num_classes + self.relu = layer.ReLU() + self.linear1 = layer.Linear(self.in_features, self.perceptron_size, bias=True) + self.linear2 = layer.Linear(self.perceptron_size, self.num_classes, bias=True) + self.softmax_cross_entropy = layer.SoftMaxCrossEntropy() + + def forward(self, inputs): + y = self.linear1(inputs) + y = self.relu(y) + y = self.linear2(y) + return y + + def train_one_batch(self, x, y, dist_option, spars): + out = self.forward(x) + loss = self.softmax_cross_entropy(out, y) + + if dist_option == 'plain': + self.optimizer(loss) + elif dist_option == 'half': + self.optimizer.backward_and_update_half(loss) + elif dist_option == 'partialUpdate': + self.optimizer.backward_and_partial_update(loss) + elif dist_option == 'sparseTopK': + self.optimizer.backward_and_sparse_update(loss, + topK=True, + spars=spars) + elif dist_option == 'sparseThreshold': + self.optimizer.backward_and_sparse_update(loss, + topK=False, + spars=spars) + return out, loss + + def set_optimizer(self, optimizer): + self.optimizer = optimizer + +def create_model(pretrained=False, **kwargs): + """Constructs a MLP model. + + Args: + pretrained (bool): If True, returns a pre-trained model. + + Returns: + The created CNN model. + """ + model = MLP(**kwargs) + + return model + + +__all__ = ['MLP', 'create_model'] \ No newline at end of file