diff --git a/docs-site/docs/installation.md b/docs-site/docs/installation.md index 2e31596..05ac360 100644 --- a/docs-site/docs/installation.md +++ b/docs-site/docs/installation.md @@ -17,7 +17,7 @@ pip install singa -f http://singa.apache.org/docs/next/wheel-cpu.html --trusted- ``` You can install a specific version of SINGA via `singa==`, where the -`` field should be replaced, e.g., `4.2.0`. The available SINGA +`` field should be replaced, e.g., `4.3.0`. The available SINGA versions are listed at the link. 2. GPU With CUDA and cuDNN @@ -28,7 +28,7 @@ pip install singa -f http://singa.apache.org/docs/next/wheel-gpu.html --trusted- ``` You can also configure SINGA version and the CUDA version, like -`singa==4.2.0+cuda10.2`. The available combinations of SINGA version and CUDA +`singa==4.3.0+cuda10.2`. The available combinations of SINGA version and CUDA version are listed at the link. Note: the Python version of your local Python environment will be used to find diff --git a/docs-site/docs/releases/RELEASE_NOTES_4.3.0.md b/docs-site/docs/releases/RELEASE_NOTES_4.3.0.md new file mode 100644 index 0000000..4c17f2d --- /dev/null +++ b/docs-site/docs/releases/RELEASE_NOTES_4.3.0.md @@ -0,0 +1,30 @@ +--- +id: RELEASE_NOTES_4.3.0 +title: Apache SINGA-4.3.0 Release Notes +--- + + + +SINGA is a distributed deep learning library. + +This release includes following changes: + + - Add the implementation for the Transformer example. + + - Enhance examples + - Update the readme file for the dynamic model slicing example. + - Update the HFL example by setting the maximum number of epochs. + - Add the multiprocess training implementation for the cnn ms example. + - Add the sparsification version of the model for the cnn ms example. + + - Extend the matrix multiplication operator to more dimensions. + + - Update the data types and tensor operations for model training. + + - Add the implementation for the new sum error loss. + + - Update the website + - Add the news for the SIGMOD Systems Award. + + - Fix bugs + - Fix the Github Actions for online code testing. diff --git a/docs-site/docs/wheel-cpu.md b/docs-site/docs/wheel-cpu.md index 4b05ff9..bd7733b 100644 --- a/docs-site/docs/wheel-cpu.md +++ b/docs-site/docs/wheel-cpu.md @@ -5,6 +5,13 @@ title: CPU only +## 4.3.0 + +- [Python 3.9](https://singa-wheel.s3.ap-southeast-1.amazonaws.com/singa-4.3.0-cp39-cp39-manylinux_2_28_x86_64.whl) +- [Python 3.10](https://singa-wheel.s3.ap-southeast-1.amazonaws.com/singa-4.3.0-cp310-cp310-manylinux_2_28_x86_64.whl) +- [Python 3.11](https://singa-wheel.s3.ap-southeast-1.amazonaws.com/singa-4.3.0-cp311-cp311-manylinux_2_28_x86_64.whl) + + ## 4.2.0 - [Python 3.9](https://singa-wheel.s3.ap-southeast-1.amazonaws.com/singa-4.2.0-cp39-cp39-manylinux_2_28_x86_64.whl) diff --git a/docs-site/docs/wheel-gpu.md b/docs-site/docs/wheel-gpu.md index 6216be3..e2d00c3 100644 --- a/docs-site/docs/wheel-gpu.md +++ b/docs-site/docs/wheel-gpu.md @@ -5,6 +5,12 @@ title: CUDA enabled +## 4.3.0 + +- [CUDA10.2, cuDNN 7.6.5, Python 3.9](https://singa-wheel.s3.ap-southeast-1.amazonaws.com/singa-4.3.0%2Bcuda10.2-cp39-cp39-manylinux_2_28_x86_64.whl) +- [CUDA10.2, cuDNN 7.6.5, Python 3.10](https://singa-wheel.s3.ap-southeast-1.amazonaws.com/singa-4.3.0%2Bcuda10.2-cp310-cp310-manylinux_2_28_x86_64.whl) +- [CUDA10.2, cuDNN 7.6.5, Python 3.11](https://singa-wheel.s3.ap-southeast-1.amazonaws.com/singa-4.3.0%2Bcuda10.2-cp311-cp311-manylinux_2_28_x86_64.whl) + ## 4.2.0 - [CUDA10.2, cuDNN 7.6.5, Python 3.9](https://singa-wheel.s3.ap-southeast-1.amazonaws.com/singa-4.2.0%2Bcuda10.2-cp39-cp39-manylinux_2_28_x86_64.whl) diff --git a/docs-site/website/blog/assets/as20240622.jpg b/docs-site/website/blog/assets/as20240622.jpg new file mode 100644 index 0000000..7e10e8e Binary files /dev/null and b/docs-site/website/blog/assets/as20240622.jpg differ diff --git a/docs-site/website/versioned_docs/version-4.3.0/autograd.md b/docs-site/website/versioned_docs/version-4.3.0/autograd.md new file mode 100644 index 0000000..e77f220 --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0/autograd.md @@ -0,0 +1,286 @@ +--- +id: version-4.3.0-autograd +title: Autograd +original_id: autograd +--- + + + +There are two typical ways to implement autograd, via symbolic differentiation +like [Theano](http://deeplearning.net/software/theano/index.html) or reverse +differentiation like +[Pytorch](https://pytorch.org/docs/stable/notes/autograd.html). SINGA follows +Pytorch way, which records the computation graph and apply the backward +propagation automatically after forward propagation. The autograd algorithm is +explained in details +[here](https://pytorch.org/docs/stable/notes/autograd.html). We explain the +relevant modules in Singa and give an example to illustrate the usage. + +## Relevant Modules + +There are three classes involved in autograd, namely `singa.tensor.Tensor`, +`singa.autograd.Operation`, and `singa.autograd.Layer`. In the rest of this +article, we use tensor, operation and layer to refer to an instance of the +respective class. + +### Tensor + +Three attributes of Tensor are used by autograd, + +- `.creator` is an `Operation` instance. It records the operation that generates + the Tensor instance. +- `.requires_grad` is a boolean variable. It is used to indicate that the + autograd algorithm needs to compute the gradient of the tensor (i.e., the + owner). For example, during backpropagation, the gradients of the tensors for + the weight matrix of a linear layer and the feature maps of a convolution + layer (not the bottom layer) should be computed. +- `.stores_grad` is a boolean variable. It is used to indicate that the gradient + of the owner tensor should be stored and output by the backward function. For + example, the gradient of the feature maps is computed during backpropagation, + but is not included in the output of the backward function. + +Programmers can change `requires_grad` and `stores_grad` of a Tensor instance. +For example, if later is set to True, the corresponding gradient is included in +the output of the backward function. It should be noted that if `stores_grad` is +True, then `requires_grad` must be true, not vice versa. + +### Operation + +It takes one or more `Tensor` instances as input, and then outputs one or more +`Tensor` instances. For example, ReLU can be implemented as a specific Operation +subclass. When an `Operation` instance is called (after instantiation), the +following two steps are executed: + +1. record the source operations, i.e., the `creator`s of the input tensors. +2. do calculation by calling member function `.forward()` + +There are two member functions for forwarding and backwarding, i.e., +`.forward()` and `.backward()`. They take `Tensor.data` as inputs (the type is +`CTensor`), and output `Ctensor`s. To add a specific operation, subclass +`operation` should implement their own `.forward()` and `.backward()`. The +`backward()` function is called by the `backward()` function of autograd +automatically during backward propogation to compute the gradients of inputs +(according to the `require_grad` field). + +### Layer + +For those operations that require parameters, we package them into a new class, +`Layer`. For example, convolution operation is wrapped into a convolution layer. +`Layer` manages (stores) the parameters and calls the corresponding `Operation`s +to implement the transformation. + +## Examples + +Multiple examples are provided in the +[example folder](https://github.com/apache/singa/tree/master/examples/autograd). +We explain two representative examples here. + +### Operation only + +The following codes implement a MLP model using only Operation instances (no +Layer instances). + +#### Import packages + +```python +from singa.tensor import Tensor +from singa import autograd +from singa import opt +``` + +#### Create weight matrix and bias vector + +The parameter tensors are created with both `requires_grad` and `stores_grad` +set to `True`. + +```python +w0 = Tensor(shape=(2, 3), requires_grad=True, stores_grad=True) +w0.gaussian(0.0, 0.1) +b0 = Tensor(shape=(1, 3), requires_grad=True, stores_grad=True) +b0.set_value(0.0) + +w1 = Tensor(shape=(3, 2), requires_grad=True, stores_grad=True) +w1.gaussian(0.0, 0.1) +b1 = Tensor(shape=(1, 2), requires_grad=True, stores_grad=True) +b1.set_value(0.0) +``` + +#### Training + +```python +inputs = Tensor(data=data) # data matrix +target = Tensor(data=label) # label vector +autograd.training = True # for training +sgd = opt.SGD(0.05) # optimizer + +for i in range(10): + x = autograd.matmul(inputs, w0) # matrix multiplication + x = autograd.add_bias(x, b0) # add the bias vector + x = autograd.relu(x) # ReLU activation operation + + x = autograd.matmul(x, w1) + x = autograd.add_bias(x, b1) + + loss = autograd.softmax_cross_entropy(x, target) + + for p, g in autograd.backward(loss): + sgd.update(p, g) +``` + +### Operation + Layer + +The following [example](https://github.com/apache/singa/blob/master/examples/autograd/mnist_cnn.py) implemeNts a CNN model using layers provided by the autograd module. + +#### Create the layers + +```python +conv1 = autograd.Conv2d(1, 32, 3, padding=1, bias=False) +bn1 = autograd.BatchNorm2d(32) +pooling1 = autograd.MaxPool2d(3, 1, padding=1) +conv21 = autograd.Conv2d(32, 16, 3, padding=1) +conv22 = autograd.Conv2d(32, 16, 3, padding=1) +bn2 = autograd.BatchNorm2d(32) +linear = autograd.Linear(32 * 28 * 28, 10) +pooling2 = autograd.AvgPool2d(3, 1, padding=1) +``` + +#### Define the forward function + +The operations in the forward pass will be recorded automatically for backward +propagation. + +```python +def forward(x, t): + # x is the input data (a batch of images) + # t is the label vector (a batch of integers) + y = conv1(x) # Conv layer + y = autograd.relu(y) # ReLU operation + y = bn1(y) # BN layer + y = pooling1(y) # Pooling Layer + + # two parallel convolution layers + y1 = conv21(y) + y2 = conv22(y) + y = autograd.cat((y1, y2), 1) # cat operation + y = autograd.relu(y) # ReLU operation + y = bn2(y) + y = pooling2(y) + + y = autograd.flatten(y) # flatten operation + y = linear(y) # Linear layer + loss = autograd.softmax_cross_entropy(y, t) # operation + return loss, y +``` + +#### Training + +```python +autograd.training = True +for epoch in range(epochs): + for i in range(batch_number): + inputs = tensor.Tensor(device=dev, data=x_train[ + i * batch_sz:(1 + i) * batch_sz], stores_grad=False) + targets = tensor.Tensor(device=dev, data=y_train[ + i * batch_sz:(1 + i) * batch_sz], requires_grad=False, stores_grad=False) + + loss, y = forward(inputs, targets) # forward the net + + for p, gp in autograd.backward(loss): # auto backward + sgd.update(p, gp) +``` + +### Using the Model API + +The following +[example](https://github.com/apache/singa/blob/master/examples/cnn/model/cnn.py) +implements a CNN model using the [Model API](./graph). + +#### Define the subclass of Model + +Define the model class, it should be the subclass of Model. In this way, all +operations used during the training phase will form a computational graph and +will be analyzed. The operations in the graph will be scheduled and executed +efficiently. Layers can also be included in the model class. + +```python +class MLP(model.Model): # the model is a subclass of Model + + def __init__(self, data_size=10, perceptron_size=100, num_classes=10): + super(MLP, self).__init__() + + # init the operators, layers and other objects + self.relu = layer.ReLU() + self.linear1 = layer.Linear(perceptron_size) + self.linear2 = layer.Linear(num_classes) + self.softmax_cross_entropy = layer.SoftMaxCrossEntropy() + + def forward(self, inputs): # define the forward function + y = self.linear1(inputs) + y = self.relu(y) + y = self.linear2(y) + return y + + def train_one_batch(self, x, y): + out = self.forward(x) + loss = self.softmax_cross_entropy(out, y) + self.optimizer(loss) + return out, loss + + def set_optimizer(self, optimizer): # attach an optimizer + self.optimizer = optimizer +``` + +#### Training + +```python +# create a model instance +model = MLP() +# initialize optimizer and attach it to the model +sgd = opt.SGD(lr=0.005, momentum=0.9, weight_decay=1e-5) +model.set_optimizer(sgd) +# input and target placeholders for the model +tx = tensor.Tensor((batch_size, 1, IMG_SIZE, IMG_SIZE), dev, tensor.float32) +ty = tensor.Tensor((batch_size, num_classes), dev, tensor.int32) +# compile the model before training +model.compile([tx], is_train=True, use_graph=True, sequential=False) + +# train the model iteratively +for b in range(num_train_batch): + # generate the next mini-batch + x, y = ... + + # Copy the data into input tensors + tx.copy_from_numpy(x) + ty.copy_from_numpy(y) + + # Training with one batch + out, loss = model(tx, ty) +``` + +#### Save a model checkpoint + +```python +# define the path to save the checkpoint +checkpointpath="checkpoint.zip" + +# save a checkpoint +model.save_states(fpath=checkpointpath) +``` + +#### Load a model checkpoint + +```python +# define the path to load the checkpoint +checkpointpath="checkpoint.zip" + +# load a checkpoint +import os +if os.path.exists(checkpointpath): + model.load_states(fpath=checkpointpath) +``` + +### Python API + +Refer +[here](https://singa.readthedocs.io/en/latest/autograd.html#module-singa.autograd) +for more details of Python API. diff --git a/docs-site/website/versioned_docs/version-4.3.0/download.md b/docs-site/website/versioned_docs/version-4.3.0/download.md new file mode 100644 index 0000000..e22e31f --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0/download.md @@ -0,0 +1,382 @@ +--- +id: version-4.3.0-downloads +title: Download SINGA +original_id: downloads +--- + + + +## Verify + +To verify the downloaded tar.gz file, download the +[KEYS](https://www.apache.org/dist/singa/KEYS) and ASC files and then execute +the following commands + +```shell +% gpg --import KEYS +% gpg --verify downloaded_file.asc downloaded_file +``` + +You can also check the SHA512 or MD5 values to see if the download is completed. +## V4.2.0 (15 March 2024): + +- [Apache SINGA 4.2.0](http://www.apache.org/dyn/closer.cgi/singa/4.2.0/apache-singa-4.2.0.tar.gz) + [\[SHA512\]](https://www.apache.org/dist/singa/4.2.0/apache-singa-4.2.0.tar.gz.sha512) + [\[ASC\]](https://www.apache.org/dist/singa/4.2.0/apache-singa-4.2.0.tar.gz.asc) +- [Release Notes 4.2.0](http://singa.apache.org/docs/releases/RELEASE_NOTES_4.2.0) +- Major changes: + + * Add support for deep learning models running on top of PolarDB + - Implement efficient model selection for a given dataset stored in the database + - Add support for dynamic model creation + - Add support for flexible setting of model training configurations + - Optimize the in-database analytics modules for scalability, efficiency and memory consumption + + * New example + - Add a horizontal federated learning example using the Bank dataset + + * Enhance examples + - Add sample training data for testing the model selection application + + * Update the website + - Update the star button in the main page + - Refine the display of star statistics + + * Update the python versions for wheel files + + * Fix bugs + - Fix the rat check files + - Update the license files + +## V4.1.0 (05 November 2023): + +- [Apache SINGA 4.1.0](https://archive.apache.org/dist/singa/4.1.0/apache-singa-4.1.0.tar.gz) + [\[SHA512\]](https://archive.apache.org/dist/singa/4.1.0/apache-singa-4.1.0.tar.gz.sha512) + [\[ASC\]](https://archive.apache.org/dist/singa/4.1.0/apache-singa-4.1.0.tar.gz.asc) +- [Release Notes 4.1.0](http://singa.apache.org/docs/releases/RELEASE_NOTES_4.1.0) +- Major changes: + + * New examples + - Add an example for malaria detection using cell images. + - Add an example for structured data learning. + + * Add support for models running on top of RDBMS + - Add support for in-database model definition and selection in RDBMS. + - Implement training-free model evaluation metrics for in-database model + selection. + - Implement a coordinator to balance between training-free and + training-based model evaluations for in-database model selection. + + * Enhance distributed training + - Add implementations for the sum error loss. + - Improve the optimizer to return model gradients. + - Improve the iterative checking for tensors and strings in the ModelMeta + class. + + * Enhance example code + - Add support for flexible setting of training configurations for models, + e.g., learning rates, weight decay, momentum, etc. + - Add implementations for dynamic models with varying layer sizes. + + * Update the website + - Add illustrations for database integration. + - Update users of Apache SINGA. + + * Fix bugs + - Update the NVIDIA_GPGKEY in the Dockerfile for building wheel files. + - Update the versions of dependencies in the wheel file. + - Fix the collections module in the model.py file. + +## V4.0.0 (07 April 2023): + +- [Apache SINGA 4.0.0](https://archive.apache.org/dist/singa/4.0.0/apache-singa-4.0.0.tar.gz) + [\[SHA512\]](https://archive.apache.org/dist/singa/4.0.0/apache-singa-4.0.0.tar.gz.sha512) + [\[ASC\]](https://archive.apache.org/dist/singa/4.0.0/apache-singa-4.0.0.tar.gz.asc) +- [Release Notes 4.0.0](http://singa.apache.org/docs/releases/RELEASE_NOTES_4.0.0) +- Major changes: + + * Enhance distributed training + - Add support for configuration of number of GPUs to be used. + - Increase max epoch for better convergence. + - Print intermediate mini-batch information. + - Add support for switching between CPU and GPU devices. + + * Enhance example code + - Update the args of normalize forward function in the transforms of the + BloodMnist example. + - Update the xceptionnet in the cnn example. + - Add arguments for weight decay, momentum and learning rates in the cnn + example. + - Add training scripts for more datasets and model types in the cnn example. + - Add resnet dist version for the large dataset cnn example. + - Add cifar 10 multi process for the large dataset cnn example. + - Add sparsification implementation for mnist in the large dataset cnn + example. + - Update the cifar datasets downloading to local directories. + - Extend the cifar datasets load function for customized directorires. + + * Enhance the webpage + - Update online documentation for distributed training. + + * Promote code quality + - Update inline comments for prepreocessing and data loading + + * Update the PIL image module + + * Update the runtime Dockerfile + + * Update the conda files + +## V3.3.0 (07 June 2022): + +- [Apache SINGA 3.3.0](https://archive.apache.org/dist/singa/3.3.0/apache-singa-3.3.0.tar.gz) + [\[SHA512\]](https://archive.apache.org/dist/singa/3.3.0/apache-singa-3.3.0.tar.gz.sha512) + [\[ASC\]](https://archive.apache.org/dist/singa/3.3.0/apache-singa-3.3.0.tar.gz.asc) +- [Release Notes 3.3.0](http://singa.apache.org/docs/releases/RELEASE_NOTES_3.3.0) +- Major changes: + * New examples + - Add one CNN example for the BloodMnist dataset, a sub set of MedMNIST. + - Add one example for the medical image analysis. + * Enhance distributed training + - Add key information printing, e.g., throughput and communication time, for + distributed training. + - Optimize printing and logging modules for faster distributed training. + * Enhance example code + - Add more datasets and model implementations for the cifar_distributed_cnn + example. + - Update the running script for the cifar_distributed_cnn example to include + more models. + - Update the dataset path for the largedataset_cnn example for more + flexibility. + - Add more model implementations for the largedataset_cnn example. + * Enhance the webpage + - Reconstruct the singa webpage to include project features. + - Update the Git web site by deploying it via .asf.yaml. + - Update the Chinese and Vietnamese documentations. + * Debug and add assertions for input tensor data types in the opt.py. + * Change pointer type to void for generalizing data types. + * Fix bugs + - Fix the python test error due to operations not implemented for some data + types. + - Fix the model of pad from bytes to str. + +## V3.2.0 (15 August 2021): + +- [Apache SINGA 3.2.0](https://archive.apache.org/dist/singa/3.2.0/apache-singa-3.2.0.tar.gz) + [\[SHA512\]](https://archive.apache.org/dist/singa/3.2.0/apache-singa-3.2.0.tar.gz.sha512) + [\[ASC\]](https://archive.apache.org/dist/singa/3.2.0/apache-singa-3.2.0.tar.gz.asc) +- [Release Notes 3.2.0](http://singa.apache.org/docs/releases/RELEASE_NOTES_3.2.0) +- Major changes: + * New examples + - Add one cifar-10 distributed CNN example for benchmarking the performance + of the distributed training. + - Add one large CNN example for training with a dataset from the filesysetm. + * Enhance distributed training + - Improve the data augmentation module for faster distributed training. + - Add device synchronization for more accurate time measurements during the + distributed training. + * Add Support for half-precision floating-point format (fp16) in deep learning + models and computational kernels. + * Update new onnx APIs and fix onnx examples accordingly, namely, DenseNet121, + ShuffleNetv1, ShuffleNetv2, SqueezeNet, VGG19. + * Add a new method to resize images by given width and height. + * Use docusaurus versioning to simplify the process of generating the project + homepage. + * Promote code quality + - Unify the formats of docstrings that describe the contents and usage of + the module. + - Unify the parameters of command-line arguments. + * Fix bugs + - Fix the CI build error by downloading the tbb binaries. + - Add disabling graph option for accessing parameter or gradient tensors + during distributed training. + - Solve the warnings of deprecated functions in the distributed optimizer + module. + +## V3.1.0 (30 October 2020): + +- [Apache SINGA 3.1.0](https://archive.apache.org/dist/singa/3.1.0/apache-singa-3.1.0.tar.gz) + [\[SHA512\]](https://archive.apache.org/dist/singa/3.1.0/apache-singa-3.1.0.tar.gz.sha512) + [\[ASC\]](https://archive.apache.org/dist/singa/3.1.0/apache-singa-3.1.0.tar.gz.asc) +- [Release Notes 3.1.0](http://singa.apache.org/docs/releases/RELEASE_NOTES_3.1.0) +- Major changes: + * Update Tensor core: + - Support tensor transformation (reshape, transpose) for tensors up to 6 + dimensions. + - Implement traverse_unary_transform in Cuda backend, which is similar to + CPP backend one. + * Add new tensor operators into the autograd module. + * Reconstruct sonnx to + - Support creating operators from both layer and autograd. + - Re-write SingaRep to provide a more powerful intermediate representation + of SINGA. + - Add a SONNXModel which implements from Model to provide uniform API and + features. + * Replace the Travis CI with Github workflow. Add quality and coverage + management. + * Add compiling and packaging scripts to create wheel packages for + distribution. + * Fix bugs + - Fix IMDB LSTM model example training script. + - Fix Tensor operation Mult on Broadcasting use cases. + - Gaussian function on Tensor now can run on Tensor with odd size. + - Updated a testing helper function gradients() in autograd to lookup param + gradient by param python object id for testing purpose. + +## V3.0.0 (18 April 2020): + +- [Apache SINGA 3.0.0](https://archive.apache.org/dist/singa/3.0.0/apache-singa-3.0.0.tar.gz) + [\[SHA512\]](https://archive.apache.org/dist/singa/3.0.0/apache-singa-3.0.0.tar.gz.sha512) + [\[ASC\]](https://archive.apache.org/dist/singa/3.0.0/apache-singa-3.0.0.tar.gz.asc) +- [Release Notes 3.0.0](http://singa.apache.org/docs/releases/RELEASE_NOTES_3.0.0) +- New features and major changes, + - Enhanced ONNX. Multiple ONNX models have been tested in SINGA. + - Distributed training with MPI and NCCL Communication optimization through + gradient sparsification and compression, and chunk transmission. + - Computational graph construction and optimization for speed and memory using + the graph. + - New documentation website (singa.apache.org) and API reference website + (apache-singa.rtfd.io). + - CI for code quality check. + - Replace MKLDNN with DNNL + - Update tensor APIs to support broadcasting operations. + - New autograd operators to support ONNX models. + +## Incubating v2.0.0 (20 April 2019): + +- [Apache SINGA 2.0.0 (incubating)](https://archive.apache.org/dist/incubator/singa/2.0.0/apache-singa-incubating-2.0.0.tar.gz) + [\[SHA512\]](https://archive.apache.org/dist/incubator/singa/2.0.0/apache-singa-incubating-2.0.0.tar.gz.sha512) + [\[ASC\]](https://archive.apache.org/dist/incubator/singa/2.0.0/apache-singa-incubating-2.0.0.tar.gz.asc) +- [Release Notes 2.0.0 (incubating)](http://singa.apache.org/docs/releases/RELEASE_NOTES_2.0.0.html) +- New features and major updates, + - Enhance autograd (for Convolution networks and recurrent networks) + - Support ONNX + - Improve the CPP operations via Intel MKL DNN lib + - Implement tensor broadcasting + - Move Docker images under Apache user name + - Update dependent lib versions in conda-build config + +## Incubating v1.2.0 (6 June 2018): + +- [Apache SINGA 1.2.0 (incubating)](https://archive.apache.org/dist/incubator/singa/1.2.0/apache-singa-incubating-1.2.0.tar.gz) + [\[SHA512\]](https://archive.apache.org/dist/incubator/singa/1.2.0/apache-singa-incubating-1.2.0.tar.gz.sha512) + [\[ASC\]](https://archive.apache.org/dist/incubator/singa/1.2.0/apache-singa-incubating-1.2.0.tar.gz.asc) +- [Release Notes 1.2.0 (incubating)](http://singa.apache.org/docs/releases/RELEASE_NOTES_1.2.0.html) +- New features and major updates, + - Implement autograd (currently support MLP model) + - Upgrade PySinga to support Python 3 + - Improve the Tensor class with the stride field + - Upgrade cuDNN from V5 to V7 + - Add VGG, Inception V4, ResNet, and DenseNet for ImageNet classification + - Create alias for conda packages + - Complete documentation in Chinese + - Add instructions for running Singa on Windows + - Update the compilation, CI + - Fix some bugs + +## Incubating v1.1.0 (12 February 2017): + +- [Apache SINGA 1.1.0 (incubating)](https://archive.apache.org/dist/incubator/singa/1.1.0/apache-singa-incubating-1.1.0.tar.gz) + [\[MD5\]](https://archive.apache.org/dist/incubator/singa/1.1.0/apache-singa-incubating-1.1.0.tar.gz.md5) + [\[ASC\]](https://archive.apache.org/dist/incubator/singa/1.1.0/apache-singa-incubating-1.1.0.tar.gz.asc) +- [Release Notes 1.1.0 (incubating)](http://singa.apache.org/docs/releases/RELEASE_NOTES_1.1.0.html) +- New features and major updates, + - Create Docker images (CPU and GPU versions) + - Create Amazon AMI for SINGA (CPU version) + - Integrate with Jenkins for automatically generating Wheel and Debian + packages (for installation), and updating the website. + - Enhance the FeedFowardNet, e.g., multiple inputs and verbose mode for + debugging + - Add Concat and Slice layers + - Extend CrossEntropyLoss to accept instance with multiple labels + - Add image_tool.py with image augmentation methods + - Support model loading and saving via the Snapshot API + - Compile SINGA source on Windows + - Compile mandatory dependent libraries together with SINGA code + - Enable Java binding (basic) for SINGA + - Add version ID in checkpointing files + - Add Rafiki toolkit for providing RESTFul APIs + - Add examples pretrained from Caffe, including GoogleNet + +## Incubating v1.0.0 (8 September 2016): + +- [Apache SINGA 1.0.0 (incubating)](https://archive.apache.org/dist/incubator/singa/1.0.0/apache-singa-incubating-1.0.0.tar.gz) + [\[MD5\]](https://archive.apache.org/dist/incubator/singa/1.0.0/apache-singa-incubating-1.0.0.tar.gz.md5) + [\[ASC\]](https://archive.apache.org/dist/incubator/singa/1.0.0/apache-singa-incubating-1.0.0.tar.gz.asc) +- [Release Notes 1.0.0 (incubating)](http://singa.apache.org/docs/releases/RELEASE_NOTES_1.0.0.html) +- New features and major updates, + - Tensor abstraction for supporting more machine learning models. + - Device abstraction for running on different hardware devices, including CPU, + (Nvidia/AMD) GPU and FPGA (to be tested in later versions). + - Replace GNU autotool with cmake for compilation. + - Support Mac OS + - Improve Python binding, including installation and programming + - More deep learning models, including VGG and ResNet + - More IO classes for reading/writing files and encoding/decoding data + - New network communication components directly based on Socket. + - Cudnn V5 with Dropout and RNN layers. + - Replace website building tool from maven to Sphinx + - Integrate Travis-CI + +## Incubating v0.3.0 (20 April 2016): + +- [Apache SINGA 0.3.0 (incubating)](https://archive.apache.org/dist/incubator/singa/0.3.0/apache-singa-incubating-0.3.0.tar.gz) + [\[MD5\]](https://archive.apache.org/dist/incubator/singa/0.3.0/apache-singa-incubating-0.3.0.tar.gz.md5) + [\[ASC\]](https://archive.apache.org/dist/incubator/singa/0.3.0/apache-singa-incubating-0.3.0.tar.gz.asc) +- [Release Notes 0.3.0 (incubating)](http://singa.apache.org/docs/releases/RELEASE_NOTES_0.3.0.html) +- New features and major updates, + - Training on GPU cluster enables training of deep learning models over a GPU + cluster. + - Python wrapper improvement makes it easy to configure the job, including + neural net and SGD algorithm. + - New SGD updaters are added, including Adam, AdaDelta and AdaMax. + - Installation has fewer dependent libraries for single node training. + - Heterogeneous training with CPU and GPU. + - Support cuDNN V4. + - Data prefetching. + - Fix some bugs. + +## Incubating v0.2.0 (14 January 2016): + +- [Apache SINGA 0.2.0 (incubating)](https://archive.apache.org/dist/incubator/singa/0.2.0/apache-singa-incubating-0.2.0.tar.gz) + [\[MD5\]](https://archive.apache.org/dist/incubator/singa/0.2.0/apache-singa-incubating-0.2.0.tar.gz.md5) + [\[ASC\]](https://archive.apache.org/dist/incubator/singa/0.2.0/apache-singa-incubating-0.2.0.tar.gz.asc) +- [Release Notes 0.2.0 (incubating)](http://singa.apache.org/docs/releases/RELEASE_NOTES_0.2.0.html) +- New features and major updates, + - Training on GPU enables training of complex models on a single node with + multiple GPU cards. + - Hybrid neural net partitioning supports data and model parallelism at the + same time. + - Python wrapper makes it easy to configure the job, including neural net and + SGD algorithm. + - RNN model and BPTT algorithm are implemented to support applications based + on RNN models, e.g., GRU. + - Cloud software integration includes Mesos, Docker and HDFS. + - Visualization of neural net structure and layer information, which is + helpful for debugging. + - Linear algebra functions and random functions against Blobs and raw data + pointers. + - New layers, including SoftmaxLayer, ArgSortLayer, DummyLayer, RNN layers and + cuDNN layers. + - Update Layer class to carry multiple data/grad Blobs. + - Extract features and test performance for new data by loading previously + trained model parameters. + - Add Store class for IO operations. + +## Incubating v0.1.0 (8 October 2015): + +- [Apache SINGA 0.1.0 (incubating)](https://archive.apache.org/dist/incubator/singa/apache-singa-incubating-0.1.0.tar.gz) + [\[MD5\]](https://archive.apache.org/dist/incubator/singa/apache-singa-incubating-0.1.0.tar.gz.md5) + [\[ASC\]](https://archive.apache.org/dist/incubator/singa/apache-singa-incubating-0.1.0.tar.gz.asc) +- [Amazon EC2 image](https://console.aws.amazon.com/ec2/v2/home?region=ap-southeast-1#LaunchInstanceWizard:ami=ami-b41001e6) +- [Release Notes 0.1.0 (incubating)](http://singa.apache.org/docs/releases/RELEASE_NOTES_0.1.0.html) +- Major features include, + - Installation using GNU build utility + - Scripts for job management with zookeeper + - Programming model based on NeuralNet and Layer abstractions. + - System architecture based on Worker, Server and Stub. + - Training models from three different model categories, namely, feed-forward + models, energy models and RNN models. + - Synchronous and asynchronous distributed training frameworks using CPU + - Checkpoint and restore + - Unit test using gtest diff --git a/docs-site/website/versioned_docs/version-4.3.0/half-precision.md b/docs-site/website/versioned_docs/version-4.3.0/half-precision.md new file mode 100644 index 0000000..1932333 --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0/half-precision.md @@ -0,0 +1,119 @@ +--- +id: version-4.3.0-half-precision +title: Half Precision +original_id: half-precision +--- + + + +Half precision training could bring benefits: + +- using less GPU memory, supporting larger network. +- training faster. + +## Half data type + +### Half data type definition + +The IEEE 754 standard specifies a binary16 as having the following +[format](https://en.wikipedia.org/wiki/Half-precision_floating-point_format): +Sign bit: 1 bit Exponent width: 5 bits Significand precision: 11 bits (10 +explicitly stored) + +### Half data type operation + +Load data in fp32 and easily convert to fp16 by casting. + +```python +>>> from singa import tensor, device +>>> dev = device.create_cuda_gpu() +>>> x = tensor.random((2,3),dev) +>>> x +[[0.7703407 0.42764223 0.5872884 ] + [0.78362167 0.70469785 0.64975065]], float32 +>>> y = x.as_type(tensor.float16) +>>> y +[[0.7705 0.4277 0.5874] + [0.7837 0.7046 0.65 ]], float16 +``` + +Primary operations are supported in fp16. + +```python +>>> y+y +[[1.541 0.8555 1.175 ] + [1.567 1.409 1.3 ]], float16 +``` + +## Training in Half + +### Training in Half three step + +Training in half precision could be done easily in three steps: + +1. Load data and convert to half +2. Set data type of optimizer +3. Train model as usual + +```python +# cast input data to fp16 +x = load_data() +x = x.astype(np.float16) +tx = tensor.from_numpy(x) + +# load model +model = build_model() +# set optimizer dtype to fp16 +sgd = opt.SGD(lr=0.1, dtype=tensor.float16) + +# train as usual +out, loss = model(tx, ty) +``` + +### Example + +An example script is `train_cnn.py`, run below command to train in half. + +```python +python examples/cnn/train_cnn.py cnn mnist -pfloat16 +``` + +## Implementation + +### Half Type Dependency + +This half implementation is integrated in C++ backend as general half type +support. + +To run on GPU, `__half` is available in Cuda math API. To support `__half` math +operation, it is required to compile against Nvidia compute arch > 6.0 (Pascal). + +### Nvidia Hardware Acceleration: Tensor Core + +Tensor Core released by Nvidia further accelerates half precision and multiples +throughput for operations like GEMM(CuBlas) and convolution(CuDNN). To enable +Tensor core operation, there are a few restriction on GEMM dimensions, +convolution channel size, Cuda version, and GPU version(Turing or later) and +etc. + +### Implement Operations + +Half operations are primarily implemented in `tensor_math_cuda.h`, by +specializing operation template with half type and implementation the low level +computation. + +For example, GEMM operation is implemented as: + +```c++ +template <> +void GEMM(const half_float::half alpha, + const Tensor& A, const Tensor& B, + const half_float::half beta, Tensor* C, + Context* ctx) { + // ... + CUBLAS_CHECK(cublasGemmEx(handle, transb, transa, ncolB, nrowA, ncolA, + alphaPtr, BPtr, Btype, ldb, APtr, Atype, lda, + betaPtr, CPtr, Ctype, ldc, computeType, algo)); + // ... +} +``` diff --git a/docs-site/website/versioned_docs/version-4.3.0/installation.md b/docs-site/website/versioned_docs/version-4.3.0/installation.md new file mode 100644 index 0000000..9ac64e0 --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0/installation.md @@ -0,0 +1,136 @@ +--- +id: version-4.3.0-installation +title: Installation +original_id: installation +--- + + + +## Using Pip + +**SINGA works with python 3.9, 3.10 and 3.11.** + +1. CPU only + [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/17RA056Brwk0vBQTFaZ-l9EbqwADO0NA9?usp=sharing) + +```bash +pip install singa -f http://singa.apache.org/docs/next/wheel-cpu.html --trusted-host singa.apache.org +``` + +You can install a specific version of SINGA via `singa==`, where the +`` field should be replaced, e.g., `4.3.0`. The available SINGA +versions are listed at the link. + +2. GPU With CUDA and cuDNN + [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1W30IPCqj5fG8ADAQsFqclaCLyIclVcJL?usp=sharing) + +```bash +pip install singa -f http://singa.apache.org/docs/next/wheel-gpu.html --trusted-host singa.apache.org +``` + +You can also configure SINGA version and the CUDA version, like +`singa==4.3.0+cuda10.2`. The available combinations of SINGA version and CUDA +version are listed at the link. + +Note: the Python version of your local Python environment will be used to find +the corresponding wheel package. For example, if your local Python is 3.9, then +the wheel package compiled on Python 3.9 will be selected by pip and installed. +In fact, the wheel file's name include SINGA version, CUDA version and Python +version. Therefore, `pip` knows which wheel file to download and install. + +Refer to the comments at the top of the `setup.py` file for how to build the +wheel packages. + +If there is no error message from + +```shell +$ python -c "from singa import tensor" +``` + +then SINGA is installed successfully. + +## Using Docker + +Install Docker on your local host machine following the +[instructions](https://docs.docker.com/install/). Add your user into the +[docker group](https://docs.docker.com/install/linux/linux-postinstall/) to run +docker commands without `sudo`. + +1. CPU-only. + +```shell +$ docker run -it apache/singa:X.Y.Z-cpu-ubuntu16.04 /bin/bash +``` + +2. With GPU enabled. Install + [Nvidia-Docker](https://github.com/NVIDIA/nvidia-docker) after install + Docker. + +```shell +$ nvidia-docker run -it apache/singa:X.Y.Z-cuda9.0-cudnn7.4.2-ubuntu16.04 /bin/bash +``` + +3. For the complete list of SINGA Docker images (tags), visit the + [docker hub site](https://hub.docker.com/r/apache/singa/). For each docker + image, the tag is named as + +```shell +version-(cpu|gpu)[-devel] +``` + +| Tag | Description | Example value | +| --------- | -------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `version` | SINGA version | '2.0.0-rc0', '2.0.0', '1.2.0' | +| `cpu` | the image cannot run on GPUs | 'cpu' | +| `gpu` | the image can run on Nvidia GPUs | 'gpu', or 'cudax.x-cudnnx.x' e.g., 'cuda10.0-cudnn7.3' | +| `devel` | indicator for development | if absent, SINGA Python package is installed for runtime only; if present, the building environment is also created, you can recompile SINGA from source at '/root/singa' | +| `OS` | indicate OS version number | 'ubuntu16.04', 'ubuntu18.04' | + +## From source + +You can [build and install SINGA](build.md) from the source code using native +building tools or conda-build, on local host OS or in a Docker container. + +## FAQ + +- Q: Error from `from singa import tensor` + + A: Check the detailed error from + + ```shell + python -c "from singa import _singa_wrap" + # go to the folder of _singa_wrap.so + ldd path to _singa_wrap.so + python + >> import importlib + >> importlib.import_module('_singa_wrap') + ``` + + The folder of `_singa_wrap.so` is like + `~/miniconda3/lib/python3.10/site-packages/singa`. Normally, the error is + caused by the mismatch or missing of dependent libraries, e.g. cuDNN or + protobuf. The solution is to create a new virtual environment and install + SINGA in that environment, e.g., + + ```shell + conda create -n singa + conda activate singa + conda install -c nusdbsystem -c conda-forge singa-cpu + ``` + +- Q: When using virtual environment, every time I install SINGA, numpy would be + reinstalled. However, the numpy is not used when I run `import numpy` + + A: It could be caused by the `PYTHONPATH` environment variable which should be + set to empty when you are using virtual environment to avoid the conflicts + with the path of the virtual environment. + +- Q: When I run SINGA in Mac OS X, I got the error "Fatal Python error: + PyThreadState_Get: no current thread Abort trap: 6" + + A: This error happens typically when you have multiple versions of Python in + your system, e.g, the one comes with the OS and the one installed by Homebrew. + The Python linked by SINGA must be the same as the Python interpreter. You can + check your interpreter by `which python` and check the Python linked by SINGA + via `otool -L `. This problem should be resolved if + SINGA is installed via conda. diff --git a/docs-site/website/versioned_docs/version-4.3.0/onnx.md b/docs-site/website/versioned_docs/version-4.3.0/onnx.md new file mode 100644 index 0000000..b3dd19c --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0/onnx.md @@ -0,0 +1,410 @@ +--- +id: version-4.3.0-onnx +title: ONNX +original_id: onnx +--- + + + +[ONNX](https://onnx.ai/) is an open representation format for machine learning +models, which enables AI developers to use models across different libraries and +tools. SINGA supports loading ONNX format models for training and inference, and +saving models defined using SINGA APIs (e.g., [Module](./module)) into ONNX +format. + +SINGA has been tested with the following +[version](https://github.com/onnx/onnx/blob/master/docs/Versioning.md) of ONNX. + +| ONNX version | File format version | Opset version ai.onnx | Opset version ai.onnx.ml | Opset version ai.onnx.training | +| ------------ | ------------------- | --------------------- | ------------------------ | ------------------------------ | +| 1.6.0 | 6 | 11 | 2 | - | + +## General usage + +### Loading an ONNX Model into SINGA + +After loading an ONNX model from disk by `onnx.load`, You only need to update +the batch-size of input using `tensor.PlaceHolder` after SINGA v3.0, the shape +of internal tensors will be inferred automatically. + +Then, you should define a class inheriting from `sonnx.SONNXModel` and implement +two methods `forward` for forward work and `train_one_batch` for training work. +After you call `model.compile`, the SONNX iterates and translates all the nodes +within the ONNX model's graph into SINGA operators, loads all stored weights and +infers each intermediate tensor's shape. + +```python3 +import onnx +from singa import device +from singa import sonnx + +class MyModel(sonnx.SONNXModel): + + def __init__(self, onnx_model): + super(MyModel, self).__init__(onnx_model) + + def forward(self, *x): + y = super(MyModel, self).forward(*x) + # Since SINGA model returns the output as a list, + # if there is only one output, + # you just need to take the first element. + return y[0] + + def train_one_batch(self, x, y): + pass + +model_path = "PATH/To/ONNX/MODEL" +onnx_model = onnx.load(model_path) + +# convert onnx model into SINGA model +dev = device.create_cuda_gpu() +x = tensor.PlaceHolder(INPUT.shape, device=dev) +model = MyModel(onnx_model) +model.compile([x], is_train=False, use_graph=True, sequential=True) +``` + +### Inference SINGA model + +Once the model is created, you can do inference by calling `model.forward`. The +input and output must be SINGA `Tensor` instances. + +```python3 +x = tensor.Tensor(device=dev, data=INPUT) +y = model.forward(x) +``` + +### Saving SINGA model into ONNX Format + +Given the input tensors and the output tensors generated by the operators the +model, you can trace back all internal operations. Therefore, a SINGA model is +defined by the input and outputs tensors. To export a SINGA model into ONNX +format, you just need to provide the input and output tensor list. + +```python3 +# x is the input tensor, y is the output tensor +sonnx.to_onnx([x], [y]) +``` + +### Re-training an ONNX model + +To train (or refine) an ONNX model using SINGA, you need to implement the +`train_one_batch` from `sonnx.SONNXModel` and mark the `is_train=True` when +calling `model.compile`. + +```python3 +from singa import opt +from singa import autograd + +class MyModel(sonnx.SONNXModel): + + def __init__(self, onnx_model): + super(MyModel, self).__init__(onnx_model) + + def forward(self, *x): + y = super(MyModel, self).forward(*x) + return y[0] + + def train_one_batch(self, x, y, dist_option, spars): + out = self.forward(x) + loss = autograd.softmax_cross_entropy(out, y) + if dist_option == 'fp32': + self.optimizer.backward_and_update(loss) + elif dist_option == 'fp16': + 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 + +sgd = opt.SGD(lr=0.005, momentum=0.9, weight_decay=1e-5) +model.set_optimizer(sgd) +model.compile([tx], is_train=True, use_graph=graph, sequential=True) +``` + +### Transfer-learning an ONNX model + +You also can append some layers to the end of the ONNX model to do +transfer-learning. The `last_layers` accept a negative integer indicating the +layer to cut off from. For example, `-1` means cut off after the final output(do +not cut off any layer), `-2` means you cut off after the last second layer. + +```python3 +from singa import opt +from singa import autograd + +class MyModel(sonnx.SONNXModel): + + def __init__(self, onnx_model): + super(MyModel, self).__init__(onnx_model) + self.linear = layer.Linear(1000, 3) + + def forward(self, *x): + # cut off after the last third layer + # and append a linear layer + y = super(MyModel, self).forward(*x, last_layers=-3)[0] + y = self.linear(y) + return y + + def train_one_batch(self, x, y, dist_option, spars): + out = self.forward(x) + loss = autograd.softmax_cross_entropy(out, y) + if dist_option == 'fp32': + self.optimizer.backward_and_update(loss) + elif dist_option == 'fp16': + 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 + +sgd = opt.SGD(lr=0.005, momentum=0.9, weight_decay=1e-5) +model.set_optimizer(sgd) +model.compile([tx], is_train=True, use_graph=graph, sequential=True) +``` + +## ONNX model zoo + +The [ONNX Model Zoo](https://github.com/onnx/models) is a collection of +pre-trained, state-of-the-art models in the ONNX format contributed by community +members. SINGA has supported several CV and NLP models now. More models are +going to be supported soon. + +### Image Classification + +This collection of models take images as input, then classifies the major +objects in the images into 1000 object categories such as keyboard, mouse, +pencil, and many animals. + +| Model Class | Reference | Description | Link | +| --------------------------------------------------------------------------------------------------- | ------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| [MobileNet](https://github.com/onnx/models/tree/master/vision/classification/mobilenet) | [Sandler et al.](https://arxiv.org/abs/1801.04381) | Light-weight deep neural network best suited for mobile and embedded vision applications.
Top-5 error from paper - ~10% | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1HsixqJMIpKyEPhkbB8jy7NwNEFEAUWAf) | +| [ResNet18](https://github.com/onnx/models/tree/master/vision/classification/resnet) | [He et al.](https://arxiv.org/abs/1512.03385) | A CNN model (up to 152 layers). Uses shortcut connections to achieve higher accuracy when classifying images.
Top-5 error from paper - ~3.6% | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1u1RYefSsVbiP4I-5wiBKHjsT9L0FxLm9) | +| [VGG16](https://github.com/onnx/models/tree/master/vision/classification/vgg) | [Simonyan et al.](https://arxiv.org/abs/1409.1556) | Deep CNN model(up to 19 layers). Similar to AlexNet but uses multiple smaller kernel-sized filters that provides more accuracy when classifying images.
Top-5 error from paper - ~8% | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/14kxgRKtbjPCKKsDJVNi3AvTev81Gp_Ds) | +| [ShuffleNet_V2](https://github.com/onnx/models/tree/master/vision/classification/shufflenet) | [Simonyan et al.](https://arxiv.org/pdf/1707.01083.pdf) | Extremely computation efficient CNN model that is designed specifically for mobile devices. This network architecture design considers direct metric such as speed, instead of indirect metric like FLOP. Top-1 error from paper - ~30.6% | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/19HfRu3YHP_H2z3BcZujVFRp23_J5XsuA?usp=sharing) | + +We also give some re-training examples by using VGG and ResNet, please check +`examples/onnx/training`. + +### Object Detection + +Object detection models detect the presence of multiple objects in an image and +segment out areas of the image where the objects are detected. + +| Model Class | Reference | Description | Link | +| ----------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- | +| [Tiny YOLOv2](https://github.com/onnx/models/tree/master/vision/object_detection_segmentation/tiny_yolov2) | [Redmon et al.](https://arxiv.org/pdf/1612.08242.pdf) | A real-time CNN for object detection that detects 20 different classes. A smaller version of the more complex full YOLOv2 network. | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/11V4I6cRjIJNUv5ZGsEGwqHuoQEie6b1T) | + +### Face Analysis + +Face detection models identify and/or recognize human faces and emotions in +given images. + +| Model Class | Reference | Description | Link | +| --------------------------------------------------------------------------------------------------------- | -------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- | +| [ArcFace](https://github.com/onnx/models/tree/master/vision/body_analysis/arcface) | [Deng et al.](https://arxiv.org/abs/1801.07698) | A CNN based model for face recognition which learns discriminative features of faces and produces embeddings for input face images. | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1qanaqUKGIDtifdzEzJOHjEj4kYzA9uJC) | +| [Emotion FerPlus](https://github.com/onnx/models/tree/master/vision/body_analysis/emotion_ferplus) | [Barsoum et al.](https://arxiv.org/abs/1608.01041) | Deep CNN for emotion recognition trained on images of faces. | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1XHtBQGRhe58PDi4LGYJzYueWBeWbO23r) | + +### Machine Comprehension + +This subset of natural language processing models that answer questions about a +given context paragraph. + +| Model Class | Reference | Description | Link | +| ----------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| [BERT-Squad](https://github.com/onnx/models/tree/master/text/machine_comprehension/bert-squad) | [Devlin et al.](https://arxiv.org/pdf/1810.04805.pdf) | This model answers questions based on the context of the given input paragraph. | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1kud-lUPjS_u-TkDAzihBTw0Vqr0FjCE-) | +| [RoBERTa](https://github.com/onnx/models/tree/master/text/machine_comprehension/roberta) | [Devlin et al.](https://arxiv.org/pdf/1907.11692.pdf) | A large transformer-based model that predicts sentiment based on given input text. | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1F-c4LJSx3Cb2jW6tP7f8nAZDigyLH6iN?usp=sharing) | +| [GPT-2](https://github.com/onnx/models/tree/master/text/machine_comprehension/gpt-2) | [Devlin et al.](https://d4mucfpksywv.cloudfront.net/better-language-models/language_models_are_unsupervised_multitask_learners.pdf) | A large transformer-based language model that given a sequence of words within some text, predicts the next word. | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1ZlXLSIMppPch6HgzKRillJiUcWn3PiK7) | + +## Supported operators + +The following operators are supported: + +- Acos +- Acosh +- Add +- And +- Asin +- Asinh +- Atan +- Atanh +- AveragePool +- BatchNormalization +- Cast +- Ceil +- Clip +- Concat +- ConstantOfShape +- Conv +- Cos +- Cosh +- Div +- Dropout +- Elu +- Equal +- Erf +- Expand +- Flatten +- Gather +- Gemm +- GlobalAveragePool +- Greater +- HardSigmoid +- Identity +- LeakyRelu +- Less +- Log +- MatMul +- Max +- MaxPool +- Mean +- Min +- Mul +- Neg +- NonZero +- Not +- OneHot +- Or +- Pad +- Pow +- PRelu +- Reciprocal +- ReduceMean +- ReduceSum +- Relu +- Reshape +- ScatterElements +- Selu +- Shape +- Sigmoid +- Sign +- Sin +- Sinh +- Slice +- Softmax +- Softplus +- Softsign +- Split +- Sqrt +- Squeeze +- Sub +- Sum +- Tan +- Tanh +- Tile +- Transpose +- Unsqueeze +- Upsample +- Where +- Xor + +### Special comments for ONNX backend + +- Conv, MaxPool and AveragePool + + Input must be 1d`(N*C*H)` and 2d(`N*C*H*W`) shape and `dilation` must be 1. + +- BatchNormalization + + `epsilon` is 1e-05 and cannot be changed. + +- Cast + + Only support float32 and int32, other types are casted to these two types. + +- Squeeze and Unsqueeze + + If you encounter errors when you `Squeeze` or `Unsqueeze` between `Tensor` and + Scalar, please report to us. + +- Empty tensor Empty tensor is illegal in SINGA. + +## Implementation + +The code of SINGA ONNX locates at `python/singa/soonx.py`. There are four main +class, `SingaFrontend`, `SingaBackend`, `SingaRep` and `SONNXModel`. +`SingaFrontend` translates a SINGA model to an ONNX model; `SingaBackend` +translates an ONNX model to `SingaRep` object which stores all SINGA operators +and tensors(the tensor in this doc means SINGA `Tensor`); `SingaRep` can be run +like a SINGA model. `SONNXModel` inherits from `model.Model` which defines a +unified API for SINGA. + +### SingaFrontend + +The entry function of `SingaFrontend` is `singa_to_onnx_model` which also is +called `to_onnx`. `singa_to_onnx_model` creates the ONNX model, and it also +create a ONNX graph by using `singa_to_onnx_graph`. + +`singa_to_onnx_graph` accepts the output of the model, and recursively iterate +the SINGA model's graph from the output to get all operators to form a queue. +The input and intermediate tensors, i.e, trainable weights, of the SINGA model +is picked up at the same time. The input is stored in `onnx_model.graph.input`; +the output is stored in `onnx_model.graph.output`; and the trainable weights are +stored in `onnx_model.graph.initializer`. + +Then the SINGA operator in the queue is translated to ONNX operators one by one. +`_rename_operators` defines the operators name mapping between SINGA and ONNX. +`_special_operators` defines which function to be used to translate the +operator. + +In addition, some operators in SINGA has different definition with ONNX, that +is, ONNX regards some attributes of SINGA operators as input, so +`_unhandled_operators` defines which function to handle the special operator. + +Since the bool type is regarded as int32 in SINGA, `_bool_operators` defines the +operators to be changed as bool type. + +### SingaBackend + +The entry function of `SingaBackend` is `prepare` which checks the version of +ONNX model and call `_onnx_model_to_singa_ops` then. + +The purpose of `_onnx_model_to_singa_ops` is to get SINGA tensors and operators. +The tensors are stored in a dictionary by their name in ONNX, and operators are +stored in queue by the form of `namedtuple('SingaOps', ['node', 'operator'])`. +For each operator, `node` is an instance from OnnxNode which is defined to store +some basic information for an ONNX node; `operator` is the SINGA operator's +forward function; + +The first step of `_onnx_model_to_singa_ops` has four steps, the first one is to +call `_parse_graph_params` to get all tensors stored as `params`. Then call +`_parse_graph_inputs_outputs` to get all input and output information stores as +`inputs` and `outputs`. Finally, it iterators all nodes within the ONNX graph +and parses it by `_onnx_node_to_singa_op` as SIGNA operators or layers and store +them as `outputs`. Some weights are stored within an ONNX node called +`Constant`, SONNX can handle them by `_onnx_constant_to_np` to store it into +`params`. + +This class finally return a `SingaRep` object and stores above `params`, +`inputs`, `outputs`, `layers`. + +### SingaRep + +`SingaBackend` stores all SINGA tensors and operators. `run` accepts the input +of the model and runs the SINGA operators one by one following the operators' +queue. The user can use `last_layers` to cut off the model after the last few +layers. + +### SONNXModel + +`SONNXModel` inherits from `sonnx.SONNXModel` and implements the method +`forward` to provide a unified API with other SINGA models. diff --git a/docs-site/website/versioned_docs/version-4.3.0/releases/RELEASE_NOTES_3.2.0.md b/docs-site/website/versioned_docs/version-4.3.0/releases/RELEASE_NOTES_3.2.0.md new file mode 100644 index 0000000..063c733 --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0/releases/RELEASE_NOTES_3.2.0.md @@ -0,0 +1,49 @@ +--- +id: version-4.3.0-RELEASE_NOTES_3.2.0 +title: Apache SINGA-3.2.0 Release Notes +original_id: RELEASE_NOTES_3.2.0 +--- + + + +SINGA is a distributed deep learning library. + +This release includes following changes: + +- New examples + + - Add one cifar-10 distributed CNN example for benchmarking the performance of + the distributed training. + - Add one large CNN example for training with a dataset from the filesysetm. + +- Enhance distributed training + + - Improve the data augmentation module for faster distributed training. + - Add device synchronization for more accurate time measurements during the + distributed training. + +- Add Support for half-precision floating-point format (fp16) in deep learning + models and computational kernels. + +- Update new onnx APIs and fix onnx examples accordingly, namely, DenseNet121, + ShuffleNetv1, ShuffleNetv2, SqueezeNet, VGG19. + +- Add a new method to resize images by given width and height. + +- Use docusaurus versioning to simplify the process of generating the project + homepage. + +- Promote code quality + + - Unify the formats of docstrings that describe the contents and usage of the + module. + - Unify the parameters of command-line arguments. + +- Fix bugs + + - Fix the CI build error by downloading the tbb binaries. + - Add disabling graph option for accessing parameter or gradient tensors + during distributed + training. + - Solve the warnings of deprecated functions in the distributed optimizer + module. diff --git a/docs-site/website/versioned_docs/version-4.3.0/releases/RELEASE_NOTES_3.2.0.rc1.md b/docs-site/website/versioned_docs/version-4.3.0/releases/RELEASE_NOTES_3.2.0.rc1.md new file mode 100644 index 0000000..5d0fb75 --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0/releases/RELEASE_NOTES_3.2.0.rc1.md @@ -0,0 +1,49 @@ +--- +id: version-4.3.0-RELEASE_NOTES_3.2.0.rc1 +title: Apache SINGA-3.2.0.rc1 Release Notes +original_id: RELEASE_NOTES_3.2.0.rc1 +--- + + + +SINGA is a distributed deep learning library. + +This release includes following changes: + +- New examples + + - Add one cifar-10 distributed CNN example for benchmarking the performance of + the distributed training. + - Add one large CNN example for training with a dataset from the filesysetm. + +- Enhance distributed training + + - Improve the data augmentation module for faster distributed training. + - Add device synchronization for more accurate time measurements during the + distributed training. + +- Add Support for half-precision floating-point format (fp16) in deep learning + models and computational kernels. + +- Update new onnx APIs and fix onnx examples accordingly, namely, DenseNet121, + ShuffleNetv1, ShuffleNetv2, SqueezeNet, VGG19. + +- Add a new method to resize images by given width and height. + +- Use docusaurus versioning to simplify the process of generating the project + homepage. + +- Promote code quality + + - Unify the formats of docstrings that describe the contents and usage of the + module. + - Unify the parameters of command-line arguments. + +- Fix bugs + + - Fix the CI build error by downloading the tbb binaries. + - Add disabling graph option for accessing parameter or gradient tensors + during distributed + training. + - Solve the warnings of deprecated functions in the distributed optimizer + module. diff --git a/docs-site/website/versioned_docs/version-4.3.0/releases/RELEASE_NOTES_3.3.0.md b/docs-site/website/versioned_docs/version-4.3.0/releases/RELEASE_NOTES_3.3.0.md new file mode 100644 index 0000000..27d5ea5 --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0/releases/RELEASE_NOTES_3.3.0.md @@ -0,0 +1,48 @@ +--- +id: version-4.3.0-RELEASE_NOTES_3.3.0 +title: Apache SINGA-3.3.0 Release Notes +original_id: RELEASE_NOTES_3.3.0 +--- + + + +SINGA is a distributed deep learning library. + +This release includes following changes: + +- New examples + + - Add one CNN example for the BloodMnist dataset, a sub set of MedMNIST. + - Add one example for the medical image analysis. + +- Enhance distributed training + + - Add key information printing, e.g., throughput and communication time, for + distributed training. + - Optimize printing and logging modules for faster distributed training. + +- Enhance example code + + - Add more datasets and model implementations for the cifar_distributed_cnn + example. + - Update the running script for the cifar_distributed_cnn example to include + more models. + - Update the dataset path for the largedataset_cnn example for more + flexibility. + - Add more model implementations for the largedataset_cnn example. + +- Enhance the webpage + + - Reconstruct the singa webpage to include project features. + - Update the Git web site by deploying it via .asf.yaml. + - Update the Chinese and Vietnamese documentations. + +- Debug and add assertions for input tensor data types in the opt.py. + +- Change pointer type to void for generalizing data types. + +- Fix bugs + + - Fix the python test error due to operations not implemented for some data + types. + - Fix the model of pad from bytes to str. diff --git a/docs-site/website/versioned_docs/version-4.3.0/releases/RELEASE_NOTES_4.0.0.md b/docs-site/website/versioned_docs/version-4.3.0/releases/RELEASE_NOTES_4.0.0.md new file mode 100644 index 0000000..dc0d23c --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0/releases/RELEASE_NOTES_4.0.0.md @@ -0,0 +1,47 @@ +--- +id: version-4.3.0-RELEASE_NOTES_4.0.0 +title: Apache SINGA-4.0.0 Release Notes +original_id: RELEASE_NOTES_4.0.0 +--- + + + +SINGA is a distributed deep learning library. + +This release includes following changes: + +- Enhance distributed training + + - Add support for configuration of number of GPUs to be used. + - Increase max epoch for better convergence. + - Print intermediate mini-batch information. + - Add support for switching between CPU and GPU devices. + +- Enhance example code + + - Update the args of normalize forward function in the transforms of the + BloodMnist example. + - Update the xceptionnet in the cnn example. + - Add arguments for weight decay, momentum and learning rates in the cnn + example. + - Add training scripts for more datasets and model types in the cnn example. + - Add resnet dist version for the large dataset cnn example. + - Add cifar 10 multi process for the large dataset cnn example. + - Add sparsification implementation for mnist in the large dataset cnn + example. + - Update the cifar datasets downloading to local directories. + - Extend the cifar datasets load function for customized directorires. + +- Enhance the webpage + + - Update online documentation for distributed training. + +- Promote code quality + + - Update inline comments for prepreocessing and data loading. + +- Update the PIL image module + +- Update the runtime Dockerfile + +- Update the conda files diff --git a/docs-site/website/versioned_docs/version-4.3.0/releases/RELEASE_NOTES_4.1.0.md b/docs-site/website/versioned_docs/version-4.3.0/releases/RELEASE_NOTES_4.1.0.md new file mode 100644 index 0000000..71deb9a --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0/releases/RELEASE_NOTES_4.1.0.md @@ -0,0 +1,47 @@ +--- +id: version-4.3.0-RELEASE_NOTES_4.1.0 +title: Apache SINGA-4.1.0 Release Notes +original_id: RELEASE_NOTES_4.1.0 +--- + + + +SINGA is a distributed deep learning library. + +This release includes following changes: + +- New examples + + - Add an example for malaria detection using cell images. + - Add an example for structured data learning. + +- Add support for models running on top of RDBMS + + - Add support for in-database model definition and selection in RDBMS. + - Implement training-free model evaluation metrics for in-database model + selection. + - Implement a coordinator to balance between training-free and training-based + model evaluations for in-database model selection. + +- Enhance distributed training + + - Add implementations for the sum error loss. + - Improve the optimizer to return model gradients. + - Improve the iterative checking for tensors and strings in the ModelMeta + class. + +- Enhance example code + + - Add support for flexible setting of training configurations for models, + e.g., learning rates, weight decay, momentum, etc. + - Add implementations for dynamic models with varying layer sizes. + +- Update the website + + - Add illustrations for database integration. + - Update users of Apache SINGA. + +- Fix bugs + - Update the NVIDIA_GPGKEY in the Dockerfile for building wheel files. + - Update the versions of dependencies in the wheel file. + - Fix the collections module in the model.py file. diff --git a/docs-site/website/versioned_docs/version-4.3.0/releases/RELEASE_NOTES_4.3.0.md b/docs-site/website/versioned_docs/version-4.3.0/releases/RELEASE_NOTES_4.3.0.md new file mode 100644 index 0000000..0165a76 --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0/releases/RELEASE_NOTES_4.3.0.md @@ -0,0 +1,31 @@ +--- +id: version-4.3.0-RELEASE_NOTES_4.3.0 +title: Apache SINGA-4.3.0 Release Notes +original_id: RELEASE_NOTES_4.3.0 +--- + + + +SINGA is a distributed deep learning library. + +This release includes following changes: + + - Add the implementation for the Transformer example. + + - Enhance examples + - Update the readme file for the dynamic model slicing example. + - Update the HFL example by setting the maximum number of epochs. + - Add the multiprocess training implementation for the cnn ms example. + - Add the sparsification version of the model for the cnn ms example. + + - Extend the matrix multiplication operator to more dimensions. + + - Update the data types and tensor operations for model training. + + - Add the implementation for the new sum error loss. + + - Update the website + - Add the news for the SIGMOD Systems Award. + + - Fix bugs + - Fix the Github Actions for online code testing. diff --git a/docs-site/website/versioned_docs/version-4.3.0/wheel-cpu.md b/docs-site/website/versioned_docs/version-4.3.0/wheel-cpu.md new file mode 100644 index 0000000..dd7b3b3 --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0/wheel-cpu.md @@ -0,0 +1,56 @@ +--- +id: version-4.3.0-wheel-cpu +title: CPU only +original_id: wheel-cpu +--- + + + +## 4.3.0 + +- [Python 3.9](https://singa-wheel.s3.ap-southeast-1.amazonaws.com/singa-4.3.0-cp39-cp39-manylinux_2_28_x86_64.whl) +- [Python 3.10](https://singa-wheel.s3.ap-southeast-1.amazonaws.com/singa-4.3.0-cp310-cp310-manylinux_2_28_x86_64.whl) +- [Python 3.11](https://singa-wheel.s3.ap-southeast-1.amazonaws.com/singa-4.3.0-cp311-cp311-manylinux_2_28_x86_64.whl) + + +## 4.2.0 + +- [Python 3.9](https://singa-wheel.s3.ap-southeast-1.amazonaws.com/singa-4.2.0-cp39-cp39-manylinux_2_28_x86_64.whl) +- [Python 3.10](https://singa-wheel.s3.ap-southeast-1.amazonaws.com/singa-4.2.0-cp310-cp310-manylinux_2_28_x86_64.whl) +- [Python 3.11](https://singa-wheel.s3.ap-southeast-1.amazonaws.com/singa-4.2.0-cp311-cp311-manylinux_2_28_x86_64.whl) + +## 4.1.0 + +- [Python 3.9](https://singa-wheel.s3.ap-southeast-1.amazonaws.com/singa-4.1.0-cp39-cp39-manylinux_2_28_x86_64.whl) +- [Python 3.10](https://singa-wheel.s3.ap-southeast-1.amazonaws.com/singa-4.1.0-cp310-cp310-manylinux_2_28_x86_64.whl) +- [Python 3.11](https://singa-wheel.s3.ap-southeast-1.amazonaws.com/singa-4.1.0-cp311-cp311-manylinux_2_28_x86_64.whl) + +## 4.0.0 + +- [Python 3.6](https://singa-wheel.s3.ap-southeast-1.amazonaws.com/singa-4.0.0-cp36-cp36m-manylinux2014_x86_64.whl) +- [Python 3.7](https://singa-wheel.s3.ap-southeast-1.amazonaws.com/singa-4.0.0-cp37-cp37m-manylinux2014_x86_64.whl) +- [Python 3.8](https://singa-wheel.s3.ap-southeast-1.amazonaws.com/singa-4.0.0-cp38-cp38-manylinux2014_x86_64.whl) + +## 3.3.0 + +- [Python 3.6](https://singa-wheel.s3.ap-southeast-1.amazonaws.com/singa-3.3.0-cp36-cp36m-manylinux2014_x86_64.whl) +- [Python 3.7](https://singa-wheel.s3.ap-southeast-1.amazonaws.com/singa-3.3.0-cp37-cp37m-manylinux2014_x86_64.whl) +- [Python 3.8](https://singa-wheel.s3.ap-southeast-1.amazonaws.com/singa-3.3.0-cp38-cp38-manylinux2014_x86_64.whl) + +## 3.2.0 + +- [Python 3.6](https://www.comp.nus.edu.sg/~dbsystem/wheelhouse/singa-3.2.0-cp36-cp36m-manylinux2014_x86_64.whl) +- [Python 3.7](https://www.comp.nus.edu.sg/~dbsystem/wheelhouse/singa-3.2.0-cp37-cp37m-manylinux2014_x86_64.whl) +- [Python 3.8](https://www.comp.nus.edu.sg/~dbsystem/wheelhouse/singa-3.2.0-cp38-cp38-manylinux2014_x86_64.whl) + +## 3.1.0 + +- [Python 3.6](https://singa-wheel.s3-ap-southeast-1.amazonaws.com/singa-3.1.0-cp36-cp36m-manylinux2014_x86_64.whl) +- [Python 3.7](https://singa-wheel.s3-ap-southeast-1.amazonaws.com/singa-3.1.0-cp37-cp37m-manylinux2014_x86_64.whl) +- [Python 3.8](https://singa-wheel.s3-ap-southeast-1.amazonaws.com/singa-3.1.0-cp38-cp38-manylinux2014_x86_64.whl) + +## 3.0.0 + +- [Python 3.6](https://singa-wheel.s3-ap-southeast-1.amazonaws.com/singa-3.0.0-cp36-cp36m-manylinux2014_x86_64.whl) +- [Python 3.7](https://singa-wheel.s3-ap-southeast-1.amazonaws.com/singa-3.0.0-cp37-cp37m-manylinux2014_x86_64.whl) +- [Python 3.8](https://singa-wheel.s3-ap-southeast-1.amazonaws.com/singa-3.0.0-cp38-cp38-manylinux2014_x86_64.whl) diff --git a/docs-site/website/versioned_docs/version-4.3.0/wheel-gpu.md b/docs-site/website/versioned_docs/version-4.3.0/wheel-gpu.md new file mode 100644 index 0000000..1f55dc2 --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0/wheel-gpu.md @@ -0,0 +1,55 @@ +--- +id: version-4.3.0-wheel-gpu +title: CUDA enabled +original_id: wheel-gpu +--- + + + +## 4.3.0 + +- [CUDA10.2, cuDNN 7.6.5, Python 3.9](https://singa-wheel.s3.ap-southeast-1.amazonaws.com/singa-4.3.0%2Bcuda10.2-cp39-cp39-manylinux_2_28_x86_64.whl) +- [CUDA10.2, cuDNN 7.6.5, Python 3.10](https://singa-wheel.s3.ap-southeast-1.amazonaws.com/singa-4.3.0%2Bcuda10.2-cp310-cp310-manylinux_2_28_x86_64.whl) +- [CUDA10.2, cuDNN 7.6.5, Python 3.11](https://singa-wheel.s3.ap-southeast-1.amazonaws.com/singa-4.3.0%2Bcuda10.2-cp311-cp311-manylinux_2_28_x86_64.whl) + +## 4.2.0 + +- [CUDA10.2, cuDNN 7.6.5, Python 3.9](https://singa-wheel.s3.ap-southeast-1.amazonaws.com/singa-4.2.0%2Bcuda10.2-cp39-cp39-manylinux_2_28_x86_64.whl) +- [CUDA10.2, cuDNN 7.6.5, Python 3.10](https://singa-wheel.s3.ap-southeast-1.amazonaws.com/singa-4.2.0%2Bcuda10.2-cp310-cp310-manylinux_2_28_x86_64.whl) +- [CUDA10.2, cuDNN 7.6.5, Python 3.11](https://singa-wheel.s3.ap-southeast-1.amazonaws.com/singa-4.2.0%2Bcuda10.2-cp311-cp311-manylinux_2_28_x86_64.whl) + +## 4.1.0 + +- [CUDA10.2, cuDNN 7.6.5, Python 3.9](https://singa-wheel.s3.ap-southeast-1.amazonaws.com/singa-4.1.0%2Bcuda10.2-cp39-cp39-manylinux_2_28_x86_64.whl) +- [CUDA10.2, cuDNN 7.6.5, Python 3.10](https://singa-wheel.s3.ap-southeast-1.amazonaws.com/singa-4.1.0%2Bcuda10.2-cp310-cp310-manylinux_2_28_x86_64.whl) +- [CUDA10.2, cuDNN 7.6.5, Python 3.11](https://singa-wheel.s3.ap-southeast-1.amazonaws.com/singa-4.1.0%2Bcuda10.2-cp311-cp311-manylinux_2_28_x86_64.whl) + +## 4.0.0 + +- [CUDA10.2, cuDNN 7.6.5, Python 3.6](https://singa-wheel.s3.ap-southeast-1.amazonaws.com/singa-4.0.0%2Bcuda10.2-cp36-cp36m-manylinux2014_x86_64.whl) +- [CUDA10.2, cuDNN 7.6.5, Python 3.7](https://singa-wheel.s3.ap-southeast-1.amazonaws.com/singa-4.0.0%2Bcuda10.2-cp37-cp37m-manylinux2014_x86_64.whl) +- [CUDA10.2, cuDNN 7.6.5, Python 3.8](https://singa-wheel.s3.ap-southeast-1.amazonaws.com/singa-4.0.0%2Bcuda10.2-cp38-cp38-manylinux2014_x86_64.whl) + +## 3.3.0 + +- [CUDA10.2, cuDNN 7.6.5, Python 3.6](https://singa-wheel.s3.ap-southeast-1.amazonaws.com/singa-3.3.0%2Bcuda10.2-cp36-cp36m-manylinux2014_x86_64.whl) +- [CUDA10.2, cuDNN 7.6.5, Python 3.7](https://singa-wheel.s3.ap-southeast-1.amazonaws.com/singa-3.3.0%2Bcuda10.2-cp37-cp37m-manylinux2014_x86_64.whl) +- [CUDA10.2, cuDNN 7.6.5, Python 3.8](https://singa-wheel.s3.ap-southeast-1.amazonaws.com/singa-3.3.0%2Bcuda10.2-cp38-cp38-manylinux2014_x86_64.whl) + +## 3.2.0 + +- [CUDA10.2, cuDNN 7.6.5, Python 3.6](https://www.comp.nus.edu.sg/~dbsystem/wheelhouse/singa-3.2.0+cuda10.2-cp36-cp36m-manylinux2014_x86_64.whl) +- [CUDA10.2, cuDNN 7.6.5, Python 3.7](https://www.comp.nus.edu.sg/~dbsystem/wheelhouse/singa-3.2.0+cuda10.2-cp37-cp37m-manylinux2014_x86_64.whl) +- [CUDA10.2, cuDNN 7.6.5, Python 3.8](https://www.comp.nus.edu.sg/~dbsystem/wheelhouse/singa-3.2.0+cuda10.2-cp38-cp38-manylinux2014_x86_64.whl) + +## 3.1.0 + +- [CUDA10.2, cuDNN 7.6.5, Python 3.6](https://singa-wheel.s3-ap-southeast-1.amazonaws.com/singa-3.1.0%2Bcuda10.2-cp36-cp36m-manylinux2014_x86_64.whl) +- [CUDA10.2, cuDNN 7.6.5, Python 3.7](https://singa-wheel.s3-ap-southeast-1.amazonaws.com/singa-3.1.0%2Bcuda10.2-cp37-cp37m-manylinux2014_x86_64.whl) +- [CUDA10.2, cuDNN 7.6.5, Python 3.8](https://singa-wheel.s3-ap-southeast-1.amazonaws.com/singa-3.1.0%2Bcuda10.2-cp38-cp38-manylinux2014_x86_64.whl) + +## 3.0.0 + +- [CUDA10.2, cuDNN 7.6.5, Python 3.6](https://singa-wheel.s3-ap-southeast-1.amazonaws.com/singa-3.0.0%2Bcuda10.2-cp36-cp36m-manylinux2014_x86_64.whl) +- [CUDA10.2, cuDNN 7.6.5, Python 3.7](https://singa-wheel.s3-ap-southeast-1.amazonaws.com/singa-3.0.0%2Bcuda10.2-cp37-cp37m-manylinux2014_x86_64.whl) +- [CUDA10.2, cuDNN 7.6.5, Python 3.8](https://singa-wheel.s3-ap-southeast-1.amazonaws.com/singa-3.0.0%2Bcuda10.2-cp38-cp38-manylinux2014_x86_64.whl) diff --git a/docs-site/website/versioned_docs/version-4.3.0_Chinese/autograd.md b/docs-site/website/versioned_docs/version-4.3.0_Chinese/autograd.md new file mode 100644 index 0000000..c5a0042 --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0_Chinese/autograd.md @@ -0,0 +1,269 @@ +--- +id: version-4.2.0_Chinese-autograd +title: Autograd +original_id: autograd +--- + + + +实现 autograd 有两种典型的方式,一种是通过 +如[Theano](http://deeplearning.net/software/theano/index.html)的符号微分 +(symbolic differentiation)或通过 +如[Pytorch](https://pytorch.org/docs/stable/notes/autograd.html)的反向微分 +(reverse differentialtion)。SINGA 遵循 Pytorch 方式,即通过记录计算图,并在正 +向传播后自动应用反向传播。自动传播算法的详细解释请参 +阅[这里](https://pytorch.org/docs/stable/notes/autograd.html)。我们接下来对 +SINGA 中的相关模块进行解释,并举例说明其使用方法。 + +## 相关模块 + +在 autograd 中涉及三个类,分别 +是`singa.tensor.Tensor`,`singa.autograd.Operation`和`singa.autograd.Layer`。在 +本篇的后续部分中,我们使用 Tensor、Operation 和 Layer 来指代这三个类。 + +### Tensor + +Tensor 的三个属性被 autograd 使用: + +- `.creator`是一个`Operation`实例。它记录了产生 Tensor 实例的这个操作。 +- `.request_grad`是一个布尔变量。它用于指示 autograd 算法是否需要计算张量的梯度 + 。例如,在反向传播的过程中,线性层的权重矩阵和卷积层(非底层)的特征图的张量梯 + 度应该被计算。 +- `.store_grad`是一个布尔变量。它用于指示张量的梯度是否应该被存储并由后向函数输 + 出。例如,特征图的梯度是在反向传播过程中计算出来的,但不包括在反向函数的输出中 + 。 + +开发者可以改变 Tensor 实例的`requires_grad`和`stores_grad`。例如,如果将后者设置 +为 True,那么相应的梯度就会被包含在后向函数的输出。需要注意的是,如 +果`stores_grad`是 True,那么 `requires_grad`一定是真,反之亦然。 + +### Operation + +它将一个或多个`Tensor`实例作为输入,然后输出一个或多个`Tensor`实例。例如,ReLU +可以作为一个具体的 Operation 子类来实现。当一个`Operation`实例被调用时(实例化后 +),会执行以下两个步骤。 + +1.记录源操作,即输入张量的`创建者`。 2.通过调用成员函数`.forward()`进行计算。 + +有两个成员函数用于前向和反向传播,即`.forward()`和`.backward()`。它们 +以`Tensor.data`作为输入(类型为`CTensor`),并输出`Ctensor`s。要添加一个特定的操 +作,子类`Operation`应该实现自己的`.forward()`和`.backward()`函数。在后向传播过程 +中,autograd 的`backward()`函数会自动调用`backward()`函数来计算输入的梯度(根 +据`require_grad`字段的参数和约束)。 + +### Layer + +对于那些需要参数的 Operation,我们把它们封装成一个新的类,`Layer`。例如,卷积操 +作被封装到卷积层(Convolution layer)中。`层`管理(存储)参数,并调用相应 +的`Operation`来实现变换。 + +## 样例 + +在[example folder](https://github.com/apache/singa/tree/master/examples/autograd)中 +提供了很多样例。在这里我我们分析两个最具代表性的例子。 + +### 只使用 Operation + +下一段代码展示了一个只使用`Operation`的多层感知机(MLP)模型: + +#### 调用依赖包 + +```python +from singa.tensor import Tensor +from singa import autograd +from singa import opt +``` + +#### 创建权重矩阵和偏置向量 + +在将`requires_grad`和`stores_grad`都设置为`True`的情况下,创建参数张量。 + +```python +w0 = Tensor(shape=(2, 3), requires_grad=True, stores_grad=True) +w0.gaussian(0.0, 0.1) +b0 = Tensor(shape=(1, 3), requires_grad=True, stores_grad=True) +b0.set_value(0.0) + +w1 = Tensor(shape=(3, 2), requires_grad=True, stores_grad=True) +w1.gaussian(0.0, 0.1) +b1 = Tensor(shape=(1, 2), requires_grad=True, stores_grad=True) +b1.set_value(0.0) +``` + +#### 训练 + +```python +inputs = Tensor(data=data) # data matrix +target = Tensor(data=label) # label vector +autograd.training = True # for training +sgd = opt.SGD(0.05) # optimizer + +for i in range(10): + x = autograd.matmul(inputs, w0) # matrix multiplication + x = autograd.add_bias(x, b0) # add the bias vector + x = autograd.relu(x) # ReLU activation operation + + x = autograd.matmul(x, w1) + x = autograd.add_bias(x, b1) + + loss = autograd.softmax_cross_entropy(x, target) + + for p, g in autograd.backward(loss): + sgd.update(p, g) +``` + +### 使用 Operation 和 Layer + +下面 +的[例子](https://github.com/apache/singa/blob/master/examples/autograd/mnist_cnn.py)使 +用 autograd 模块提供的层实现了一个 CNN 模型。 + +#### 创建层 + +```python +conv1 = autograd.Conv2d(1, 32, 3, padding=1, bias=False) +bn1 = autograd.BatchNorm2d(32) +pooling1 = autograd.MaxPool2d(3, 1, padding=1) +conv21 = autograd.Conv2d(32, 16, 3, padding=1) +conv22 = autograd.Conv2d(32, 16, 3, padding=1) +bn2 = autograd.BatchNorm2d(32) +linear = autograd.Linear(32 * 28 * 28, 10) +pooling2 = autograd.AvgPool2d(3, 1, padding=1) +``` + +#### 定义正向传播函数 + +在正向传播中的 operations 会被自动记录,用于反向传播。 + +```python +def forward(x, t): + # x is the input data (a batch of images) + # t is the label vector (a batch of integers) + y = conv1(x) # Conv layer + y = autograd.relu(y) # ReLU operation + y = bn1(y) # BN layer + y = pooling1(y) # Pooling Layer + + # two parallel convolution layers + y1 = conv21(y) + y2 = conv22(y) + y = autograd.cat((y1, y2), 1) # cat operation + y = autograd.relu(y) # ReLU operation + y = bn2(y) + y = pooling2(y) + + y = autograd.flatten(y) # flatten operation + y = linear(y) # Linear layer + loss = autograd.softmax_cross_entropy(y, t) # operation + return loss, y +``` + +#### 训练 + +```python +autograd.training = True +for epoch in range(epochs): + for i in range(batch_number): + inputs = tensor.Tensor(device=dev, data=x_train[ + i * batch_sz:(1 + i) * batch_sz], stores_grad=False) + targets = tensor.Tensor(device=dev, data=y_train[ + i * batch_sz:(1 + i) * batch_sz], requires_grad=False, stores_grad=False) + + loss, y = forward(inputs, targets) # forward the net + + for p, gp in autograd.backward(loss): # auto backward + sgd.update(p, gp) +``` + +### Using the Model API + +下面 +的[样例](https://github.com/apache/singa/blob/master/examples/cnn/model/cnn.py)使 +用[Model API](./graph)实现了一个 CNN 模型。. + +#### 定义 Model 的子类 + +定义模型类,它应该是 Model 的子类。只有这样,在训练阶段使用的所有操作才会形成一 +个计算图以便进行分析。图中的操作将被按时序规划并有效执行,模型类中也可以包含层。 + +```python +class MLP(model.Model): # the model is a subclass of Model + + def __init__(self, data_size=10, perceptron_size=100, num_classes=10): + super(MLP, self).__init__() + + # init the operators, layers and other objects + self.relu = layer.ReLU() + self.linear1 = layer.Linear(perceptron_size) + self.linear2 = layer.Linear(num_classes) + self.softmax_cross_entropy = layer.SoftMaxCrossEntropy() + + def forward(self, inputs): # define the forward function + y = self.linear1(inputs) + y = self.relu(y) + y = self.linear2(y) + return y + + def train_one_batch(self, x, y): + out = self.forward(x) + loss = self.softmax_cross_entropy(out, y) + self.optimizer(loss) + return out, loss + + def set_optimizer(self, optimizer): # attach an optimizer + self.optimizer = optimizer +``` + +#### 训练 + +```python +# create a model instance +model = MLP() +# initialize optimizer and attach it to the model +sgd = opt.SGD(lr=0.005, momentum=0.9, weight_decay=1e-5) +model.set_optimizer(sgd) +# input and target placeholders for the model +tx = tensor.Tensor((batch_size, 1, IMG_SIZE, IMG_SIZE), dev, tensor.float32) +ty = tensor.Tensor((batch_size, num_classes), dev, tensor.int32) +# compile the model before training +model.compile([tx], is_train=True, use_graph=True, sequential=False) + +# train the model iteratively +for b in range(num_train_batch): + # generate the next mini-batch + x, y = ... + + # Copy the data into input tensors + tx.copy_from_numpy(x) + ty.copy_from_numpy(y) + + # Training with one batch + out, loss = model(tx, ty) +``` + +#### 保存模型 checkpoint + +```python +# define the path to save the checkpoint +checkpointpath="checkpoint.zip" + +# save a checkpoint +model.save_states(fpath=checkpointpath) +``` + +#### 加载模型 checkpoint + +```python +# define the path to load the checkpoint +checkpointpath="checkpoint.zip" + +# load a checkpoint +import os +if os.path.exists(checkpointpath): + model.load_states(fpath=checkpointpath) +``` + +### Python API + +关于 Python API 的更多细节,请参 +考[这里](https://singa.readthedocs.io/en/latest/autograd.html#module-singa.autograd)。 diff --git a/docs-site/website/versioned_docs/version-4.3.0_Chinese/benchmark-train.md b/docs-site/website/versioned_docs/version-4.3.0_Chinese/benchmark-train.md new file mode 100644 index 0000000..695cd51 --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0_Chinese/benchmark-train.md @@ -0,0 +1,23 @@ +--- +id: version-4.2.0_Chinese-benchmark-train +title: Benchmark for Distributed Training +original_id: benchmark-train +--- + + + +项目:我们使用深度卷积神经网 +络[ResNet-50](https://github.com/apache/singa/blob/master/examples/cnn/model/resnet.py)。 +它有 50 个卷积层,用于图像分类。它需要 3.8 个 GFLOPs 来通过网络处理一张图像(尺 +寸为 224x224)。输入的图像大小为 224x224。 + +硬件方面:我们使用的是 AWS 的 p2.8xlarge 实例,每个实例有 8 个 Nvidia Tesla K80 +GPU,共 96GB GPU 内存,32 个 vCPU,488GB 主内存,10Gbps 网络带宽。 + +衡量标准:我们衡量不同数量 worker 的每次迭代时间,以评估 SINGA 的可扩展性 +。Batch-size 固定为每个 GPU32 个。采用同步训练方案。因此,有效的 batch-size +是`32N`,其中 N 是 GPU 的数量。我们与一个流行的开源系统进行比较,该系统采用参数 +服务器拓扑结构。选择第一个 GPU 作为服务器。 + +![Benchmark Experiments](assets/benchmark.png)
**可扩展性测试。条形为吞吐 +量,折线形为通信成本。** diff --git a/docs-site/website/versioned_docs/version-4.3.0_Chinese/build.md b/docs-site/website/versioned_docs/version-4.3.0_Chinese/build.md new file mode 100644 index 0000000..7c39526 --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0_Chinese/build.md @@ -0,0 +1,492 @@ +--- +id: version-4.2.0_Chinese-build +title: Build SINGA from Source +original_id: build +--- + + + +源文件可以通过[tar.gz 文件](https://dist.apache.org/repos/dist/dev/singa/)或 git +repo 的形式下载: + +```shell +$ git clone https://github.com/apache/singa.git +$ cd singa/ +``` + +如果您想为 SINGA 贡献代码,请参考[贡献代码](contribute-code.md)页面的步骤和要求 +。 + +## 使用 Conda 构筑 SINGA + +Conda-build 是一个构建工具,它从 anaconda cloud 安装依赖的库并执行构建脚本。 + +安装 conda-build(需要先安装 conda): + +```shell +conda install conda-build +``` + +### 构建 CPU 版本 + +构建 SINGA 的 CPU 版本: + +```shell +conda build tool/conda/singa/ +``` + +以上命令已经在 Ubuntu(14.04,16.04 和 18.04)和 macOS 10.11 上测试过。更多信息 +请参考[Travis-CI page](https://travis-ci.org/apache/singa)页面。 + +### 构建 GPU 版本 + +要构建 GPU 版的 SINGA,计算机必须装备有 Nvida GPU,而且需要安装 CUDA +driver(>=384.81)、CUDA toolkit(>=9)和 cuDNN(>=7)。以下两个 Docker 镜像提供了构建 +环境: + +1. apache/singa:conda-cuda9.0 +2. apache/singa:conda-cuda10.0 + +构建环境准备好后,需要先导出 CUDA 版本,然后运行 conda 命令构建 SINGA: + +```shell +export CUDA=x.y (e.g. 9.0) +conda build tool/conda/singa/ +``` + +### 后处理 + +生成的包文件的位置(`.tar.gz`)将打印在终端上,生成的包可以直接安装: + +```shell +conda install -c conda-forge --use-local +``` + +若要上传到 anaconda 云端供他人下载安装,需要在 anaconda 上注册一个账号,才 +能[上传包](https://docs.anaconda.com/anaconda-cloud/user-guide/getting-started/): + +```shell +conda install anaconda-client +anaconda login +anaconda upload -l main +``` + +将包上传到云端后,您可以在[Anaconda Cloud](https://anaconda.org/)上看到,也可以 +通过以下命令查看: + +```shell +conda search -c singa +``` + +每个特定的 SINGA 软件包都由版本和构建字符串来标识。要安装一个特定的 SINGA 包,需 +要提供所有信息,例如: + +```shell +conda install -c -c conda-forge singa=2.1.0.dev=cpu_py36 +``` + +为了使安装命令简单化,您可以创建以下附加包,这些包依赖于最新的 CPU 和 GPU SINGA +包: + +```console +# for singa-cpu +conda build tool/conda/cpu/ --python=3.6 +conda build tool/conda/cpu/ --python=3.7 +# for singa-gpu +conda build tool/conda/gpu/ --python=3.6 +conda build tool/conda/gpu/ --python=3.7 +``` + +因此,当您运行: + +```shell +conda install -c -c conda-forge singa-xpu +``` + +时(`xpu`表示'cpu' or 'gpu'), 相应的真正的 SINGA 包将作为依赖库被安装。 + +## 使用本地工具在 Ubuntu 上构建 SINGA + +请参阅 SINGA +[Dockerfiles](https://github.com/apache/singa/blob/master/tool/docker/devel/ubuntu/cuda9/Dockerfile#L30), +了解在 Ubuntu 16.04 上安装依赖库的说明。您也可以使用 devel 映像创建一个 Docker +容器,并在容器中构建 SINGA。要使用 GPU、DNNL、Python 和单元测试来构建 SINGA,请 +运行以下命令: + +```shell +mkdir build # at the root of singa folder +cd build +cmake -DENABLE_TEST=ON -DUSE_CUDA=ON -DUSE_DNNL=ON -DUSE_PYTHON3=ON .. +make +cd python +pip install . +``` + +CMake 选项的详细内容在本页最后一节解释,上面最后一条命令是安装 Python 包。你也可 +以运行`pip install -e .`,它可以创建符号链接,而不是将 Python 文件复制到 +site-package 文件夹中。 + +如果 SINGA 在 ENABLE_TEST=ON 的情况下编译,您可以通过以下方式运行单元测试: + +```shell +$ ./bin/test_singa +``` + +您可以看到所有的测试案例与测试结果。如果 SINGA 通过了所有测试,那么您就成功安装 +了 SINGA。 + +## 使用本地工具在 Centos7 上构建 SINGA + +由于 Centos7 的软件包名称不同,因此从源码开始构建会有所不同。 + +### 安装依赖项 + +基础包和库文件: + +```shell +sudo yum install freetype-devel libXft-devel ncurses-devel openblas-devel blas-devel lapack devel atlas-devel kernel-headers unzip wget pkgconfig zip zlib-devel libcurl-devel cmake curl unzip dh-autoreconf git python-devel glog-devel protobuf-devel +``` + +构建必需的包: + +```shell +sudo yum group install "Development Tools" +``` + +若要安装 swig: + +```shell +sudo yum install pcre-devel +wget http://prdownloads.sourceforge.net/swig/swig-3.0.10.tar.gz +tar xvzf swig-3.0.10.tar.gz +cd swig-3.0.10.tar.gz +./configure --prefix=${RUN} +make +make install +``` + +安装 gfortran: + +```shell +sudo yum install centos-release-scl-rh +sudo yum --enablerepo=centos-sclo-rh-testing install devtoolset-7-gcc-gfortran +``` + +安装 pip 和其他包: + +```shell +sudo yum install epel-release +sudo yum install python-pip +pip install matplotlib numpy pandas scikit-learn pydot +``` + +### 安装 SINGA + +按照《使用本地工具在 Ubuntu 上构建 SINGA》的步骤 1-5 进行操作 + +### 测试 + +您可以通过如下方式进行 + +```shell +$ ./bin/test_singa +``` + +您可以看到所有的测试案例与测试结果。如果 SINGA 通过了所有测试,即表示安装成功。 + +## 在 Windows 中编译 SINGA + +在 Windows 上使用 Python 支持构建 SINGA 的说明可以 +在[install-win 页面](install-win.md)找到。 + +## 关于编译选项的更多细节 + +### USE_MODULES (已过期废弃) + +如果没有安装 protobuf 和 openblas,你可以用它们一起编译 SINGA + +```shell +$ In SINGA ROOT folder +$ mkdir build +$ cd build +$ cmake -DUSE_MODULES=ON .. +$ make +``` + +cmake 会下载 OpenBlas 和 Protobuf (2.6.1) 并与 SINGA 一起编译。 + +您可以使用`ccmake ..`来配置编译选项。如果一些依赖的库不在系统默认路径中,则您需 +要手动导出以下环境变量: + +```shell +export CMAKE_INCLUDE_PATH= +export CMAKE_LIBRARY_PATH= +``` + +### USE_PYTHON + +编译 SINGA 的 Python 封装器选项: + +```shell +$ cmake -DUSE_PYTHON=ON .. +$ make +$ cd python +$ pip install . +``` + +### USE_CUDA + +我们推荐用户安装 CUDA 和[cuDNN](https://developer.nvidia.com/cudnn),以便在 GPU +上运行 SINGA,以获得更好的性能。 + +SINGA 已经在 CUDA 9/10 和 cuDNN 7 上进行了测试。如果 cuDNN 安装在非系统文件夹中 +,例如 /home/bob/local/cudnn/,则需要执行以下命令来让 cmake 在编译时能够找到它们 +: + +```shell +$ export CMAKE_INCLUDE_PATH=/home/bob/local/cudnn/include:$CMAKE_INCLUDE_PATH +$ export CMAKE_LIBRARY_PATH=/home/bob/local/cudnn/lib64:$CMAKE_LIBRARY_PATH +$ export LD_LIBRARY_PATH=/home/bob/local/cudnn/lib64:$LD_LIBRARY_PATH +``` + +CUDA 和 cuDNN 的 cmake 选项应该设置成“ON”: + +```shell +# Dependent libs are install already +$ cmake -DUSE_CUDA=ON .. +$ make +``` + +### USE_DNNL + +用户可以启用 DNNL 来提高 CPU 的计算性能,DNNL 的安装指南可以 +在[这里](https://github.com/intel/mkl-dnn#installation)找到: + +SINGA 在 DNNL v1.1 环境下已经进行过测试并通过, + +若要启用 DNNL 支持来编译 SINGA: + +```shell +# Dependent libs are installed already +$ cmake -DUSE_DNNL=ON .. +$ make +``` + +### USE_OPENCL + +SINGA 使用 opencl-headers 和 viennacl(版本 1.7.1 及以上)来支持 OpenCL,它们可 +以通过如下方式安装: + +```shell +# On Ubuntu 16.04 +$ sudo apt-get install opencl-headers, libviennacl-dev +# On Fedora +$ sudo yum install opencl-headers, viennacl +``` + +此外,你需要在你想运行 OpenCL 的平台安装 OpenCL Installable Client Driver(ICD) +。 + +- 对于 AMD 和 Nvidia 的 GPU,驱动包也应该安装与之匹配的 OpenCL ICD。 +- 对于 Intel 的 CPU 和/或 GPU,请 + 从[Intel 官方网站](https://software.intel.com/en-us/articles/opencl-drivers)上 + 获取驱动程序。请注意,该网站上提供的驱动程序只支持最新的 CPU 和 Iris GPU。 +- 对于旧的 Intel CPU,你可以使用 beignet-opencl-icd 包。 + +请注意,目前不建议在 CPU 上运行 OpenCL,因为运行速度会很慢。内存传输是以整数秒为 +单位的(直观来说,CPU 上是 1000 毫秒,而 GPU 上是 1 毫秒)。 + +更多关于建立 OpenCL 工作环境的信息可以 +在[这里](https://wiki.tiker.net/OpenCLHowTo)找到。 + +如果 ViennaCL 的软件包版本不是至少 1.7.1,则需要从源码构建它: + +从[这个 git repo](https://github.com/viennacl/viennacl-dev)clone 版本库,切换 +(checkout)到 release-1.7.1 分支,然后构建它,并把项目路径添加到 PATH,再把构建 +的库文件添加到 LD_LIBRARY_PATH。 + +构建支持 OpenCL 的 SINGA(在 SINGA 1.1 上测试): + +```shell +$ cmake -DUSE_OPENCL=ON .. +$ make +``` + +### PACKAGE + +这个设置是用来构建 Debian 软件包的。设置 PACKAGE=ON,然后用 make 命令来编译软件 +包,如下所示: + +```shell +$ cmake -DPACKAGE=ON +$ make package +``` + +## FAQ + +- Q: 'import singa'阶段报错 + + A: 请检查`python -c "from singa import _singa_wrap`中的详细错误。有时是由依赖 + 库引起的,比如 protobuf 有多个版本,cudnn 缺失,numpy 版本不匹配等问题。下面展 + 示了不同情况下的解决方案 + + 1. 检查 cudnn 和 cuda。如果 cudnn 缺失或与 wheel 包的版本不一致,你可以下载正 + 确的 cudnn 版本到~/local/cudnn/,然后: + + ```shell + $ echo "export LD_LIBRARY_PATH=/home//local/cudnn/lib64:$LD_LIBRARY_PATH" >> ~/.bashrc + ``` + + 2. 如果是 protobuf 的问题。你可以将 protobuf (3.6.1)从源码安装到本地文件夹,比 + 如 ~/local/,解压 tar 文件,然后: + + ```shell + $ ./configure --prefix=/home/local + $ make && make install + $ echo "export LD_LIBRARY_PATH=/home//local/lib:$LD_LIBRARY_PATH" >> ~/.bashrc + $ source ~/.bashrc + ``` + + 3. 如果找不到包括 python 在内的其他类库,则使用`pip`或`conda`创建虚拟环境. + + 4. 如果不是上述原因造成的,请到`_singa_wrap.so`文件夹中查看: + + ```shell + $ python + >> import importlib + >> importlib.import_module('_singa_wrap') + ``` + + 来检查错误信息。例如,如果是 numpy 的版本不匹配,错误信息会是: + + ```shell + RuntimeError: module compiled against API version 0xb but this version of numpy is 0xa + ``` + + 那么你就需要更新 numpy 到更高版本。 + +* Q: 运行`cmake ...`时出错,找不到依赖库。 + + A: 如果你还没有安装这些依赖库,请先安装它们。如果你在系统文件夹之外的文件夹中 + 安装了库,例如/usr/local,那您需要手动导出以下变量: + + ```shell + $ export CMAKE_INCLUDE_PATH= + $ export CMAKE_LIBRARY_PATH= + ``` + +- Q: 来自`make`的错误,例如 linking 阶段的错误. + + A: 如果您的库文件在系统默认路径以外的其他文件夹中,则需要手动导出以下变量。 + + ```shell + $ export LIBRARY_PATH= + $ export LD_LIBRARY_PATH= + ``` + +* Q: 来自头文件的错误,例如'cblas.h 文件不存在' + + A: 您需要手动将 cblas.h 的文件夹包含在 CPLUS_INCLUDE_PATH 中,例如: + + ```shell + $ export CPLUS_INCLUDE_PATH=/opt/OpenBLAS/include:$CPLUS_INCLUDE_PATH + ``` + +* Q: 在编译 SINGA 时,我收到错误信息`SSE2 instruction set not enabled` + + A:您可以尝试如下指令: + + ```shell + $ make CFLAGS='-msse2' CXXFLAGS='-msse2' + ``` + +* Q:当试图导入.py 文件时,从 google.protobuf.internal 收 + 到`ImportError: cannot import name enum_type_wrapper`。 + + A: 您需要安装 protobuf 的 Python 绑定包,它可以通过如下方式安装: + + ```shell + $ sudo apt-get install protobuf + ``` + + 或从源文件编译: + + ```shell + $ cd /PROTOBUF/SOURCE/FOLDER + $ cd python + $ python setup.py build + $ python setup.py install + ``` + +* Q: 当我从源码构建 OpenBLAS 时,被告知需要一个 Fortran 编译器。 + + A: 您可以通过如下方式编译 OpenBLAS: + + ```shell + $ make ONLY_CBLAS=1 + ``` + + 或者通过如下方式安装: + + ```shell + $ sudo apt-get install libopenblas-dev + ``` + +* Q: 当我构建协议缓冲区时,它报告说在`/usr/lib64/libstdc++.so.6`中没有找 + 到`GLIBC++_3.4.20`? + + A: 这意味着链接器找到了 libstdc++.so.6,但该库属于旧的 GCC(用于编译和链接程序 + 的 GCC)版本。此程序依赖于新的 libstdc++中定义的代码,而该代码属于较新版本的 + GCC,所以必须告诉链接器如何找到较新的 libstdc++共享库。最简单的解决方法是找到 + 正确的 libstdc++,并将其导出到 LD_LIBRARY_PATH。例如,如果下面命令的输出中列出 + 了 GLIBC++\_3.4.20: + + $ strings /usr/local/lib64/libstdc++.so.6|grep GLIBC++ + + 那么接下来需要设置环境变量为: + + $ export LD_LIBRARY_PATH=/usr/local/lib64:$LD_LIBRARY_PATH + +* Q: 当构建 glog 时,报告说 "src/logging_unittest.cc:83:20: error: 'gflags' is + not a namespace-name"。 + + A: 可能是由于安装的 gflags 用了不同的命名空间,比如 "google",所以 glog 找不到 + 'gflags'的命名空间。实际上建立 glog 并不需要 gflags,所以你可以修改 + configure.ac 文件来忽略 gflags。 + + 1. cd to glog src directory + 2. change line 125 of configure.ac to "AC_CHECK_LIB(gflags, main, ac_cv_have_libgflags=0, ac_cv_have_libgflags=0)" + 3. autoreconf + + 执行上述命令后,就可以重新构建 glog 了。 + +* Q: 在使用虚拟环境时,每次运行 pip install,都会重新安装 numpy。但是,当运 + 行`import numpy`时,numpy 并没有被调用。 + + A: 可能是由于`PYTHONPATH`造成的,在使用虚拟环境时,应将`PYTHONPATH`设置为空, + 以避免与虚拟环境本身的路径冲突。 + +* Q: 当从源代码编译 PySINGA 时,由于缺少,出现的编译错误。 + + A: 请安装 numpy 并导出 numpy 头文件的路径为 + + $ export CPLUS_INCLUDE_PATH=`python -c "import numpy; print numpy.get_include()"`:$CPLUS_INCLUDE_PATH + +* Q: 当我在 Mac OS X 中运行 SINGA 时,报错 "Fatal Python error: + PyThreadState_Get: no current thread Abort trap: 6" + + A: 这个错误通常发生在系统上有多个版本的 Python,并且是通过 pip 安装的 SINGA ( + 通过 conda 安装时不会出现这个问题),例如,操作系统自带的版本和 Homebrew 安装的 + 版本。PySINGA 所链接的 Python 必须与 Python 解释器(interpreter)相同。 您可以 + 通过 `which python` 检查您的解释器路径并通 + 过`otool -L `检查 PySINGA 链接的 Python 路径。要解决这 + 个问题,请用正确的 Python 版本编译 SINGA。需要注意的是,如果您从源码编译 + PySINGA,您需要在调 + 用[cmake](http://stackoverflow.com/questions/15291500/i-have-2-versions-of-python-installed-but-cmake-is-using-older-version-how-do)时 + 指定路径: + + + $ cmake -DPYTHON_LIBRARY=`python-config --prefix`/lib/libpython2.7.dylib -DPYTHON_INCLUDE_DIR=`python-config --prefix`/include/python2.7/ .. + +如果从二进制包中安装 PySINGA,例如 debian 或 wheel,那么你需要改变 python 解释器 +的路径,例如,重新设置\$PATH,并把 Python 的正确路径放在前面的位置。 diff --git a/docs-site/website/versioned_docs/version-4.3.0_Chinese/contribute-code.md b/docs-site/website/versioned_docs/version-4.3.0_Chinese/contribute-code.md new file mode 100644 index 0000000..58bdac2 --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0_Chinese/contribute-code.md @@ -0,0 +1,122 @@ +--- +id: version-4.2.0_Chinese-contribute-code +title: How to Contribute Code +original_id: contribute-code +--- + + + +## 代码风格 + +SINGA 代码库 +在[CPP](http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml)和[Python](http://google.github.io/styleguide/pyguide.html)代 +码中都遵循 Google 风格。 + +强制执行 Google 编码风格的一个简单方法是使用 Visual Studio Code 编辑器中的 +linting 和格式化工具: + +- [C/C++扩展](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools) +- [Python 扩展](https://marketplace.visualstudio.com/items?itemName=ms-python.python) +- [cpplint 扩展](https://marketplace.visualstudio.com/items?itemName=mine.cpplint) +- [Clang-Format](https://marketplace.visualstudio.com/items?itemName=xaver.clang-format) + +安装扩展后,编辑`settings.json`文件: + +```json +{ + "[cpp]": { + "editor.defaultFormatter": "xaver.clang-format" + }, + "cpplint.cpplintPath": "path/to/cpplint", + + "editor.formatOnSave": true, + "python.formatting.provider": "yapf", + "python.linting.enabled": true, + "python.linting.lintOnSave": true, + "clang-format.language.cpp.style": "google", + "python.formatting.yapfArgs": ["--style", "{based_on_style: google}"] +} +``` + +根据您的操作系统,用户设置文件位于以下位置: + +1. Windows %APPDATA%\Code\User\settings.json +2. macOS "\$HOME/Library/Application Support/Code/User/settings.json" +3. Linux "\$HOME/.config/Code/User/settings.json" + +配置是在相应的配置文件中指定的。而这些工具会自动查找项目根目录下的配置文件,比 +如`.pylintrc`。 + +#### 安装必要工具 + +最理想的情况是所有贡献者都使用相同版本的代码格式化工具(clang-format 9.0.0 和 +yapf 0.29.0),这样在不同 PR 中的代码格式化就会完全相同,从而摆脱 github pull +request 冲突。 + +首先,安装 LLVM 9.0,它提供了 clang-format 9.0.0 版本,LLVM 的下载页面如下: + +- [LLVM](http://releases.llvm.org/download.html#9.0.0) + + - Ubuntu 系统: + + ```sh + sudo apt-get install clang-format-9 + ``` + + - Windows 系统,下载预编译包并安装。 + +然后,安装 cpplint, pylint 和 yapf + +- Ubuntu 或 OSX: + + ``` + $ sudo pip install cpplint + $ which cpplint + /path/to/cpplint + + $ pip install yapf==0.29.0 + $ pip install pylint + ``` + +- Windows: 安装 Anaconda 进行包管理 + + ``` + $ pip install cpplint + $ where cpplint + C:/path/to/cpplint.exe + + $ pip install yapf==0.29.0 + $ pip install pylint + ``` + +#### 使用 + +- 配置后,在编辑源代码文件时,linting 会自动启用。错误和警告会在 Visual Studio + Code `PROBLEMS`面板中列出。 +- 代码格式化可以通过调出 Command Palette(Windows 中为`Shift+Ctrl+P`,OS X 中 + 为`Shift+Command+P`)并输入`Format Document`来完成。 + +#### 提交 + +修正格式错误以后就可以提交 pull request 了。 + +## 开发环境 + +推荐使用 Visual Studio Code 作为编辑器。可以安装 Python、C/C++、代码拼写检查器 +、autoDocstring、vim、远程开发等扩展。这些扩展的参考配置(即`settings.json`)可 +以在[这里](https://gist.github.com/nudles/3d23cfb6ffb30ca7636c45fe60278c55)查看 +。 + +如果更新 CPP 代码,需要从[源文件](./build.md)重新编译 SINGA。建议使 +用`*-devel Docker`镜像中的原生构建工具或使用`conda build`。 + +如果要只更新 Python 代码,您可以安装一次 SINGA,然后复制更新后的 Python 文件来替 +换 Python 安装文件夹中的文件。 + +```shell +cp python/singa/xx.py /lib/python3.7/site-packages/singa/ +``` + +## 工作流程 + +请参阅[git 工作流程页面](./git-workflow.md). diff --git a/docs-site/website/versioned_docs/version-4.3.0_Chinese/contribute-docs.md b/docs-site/website/versioned_docs/version-4.3.0_Chinese/contribute-docs.md new file mode 100644 index 0000000..9c917e7 --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0_Chinese/contribute-docs.md @@ -0,0 +1,100 @@ +--- +id: version-4.2.0_Chinese-contribute-docs +title: How to Contribute to Documentation +original_id: contribute-docs +--- + + + +文档有两种类型,即 markdown 文件和 API 使用参考。本指南介绍了一些工具,并指导如 +何准备 md 文件和 API 注释。 + +md 文件将通过[Docusaurus](https://docusaurus.io/)构建成 HTML 页面;API 注释(来 +自源代码)将用于使用 Sphinx(对应 Python)和 Doxygen(对应 CPP)生成 API 参考页 +面。 + +## Markdown 文件 + +请尽量遵循[Google Documentation style](https://developers.google.com/style)。例 +如: + +1. 删除指令中的"please"。如:'Please click...' VS 'Click...'。 +2. 遵循标准 + 的[大小写规则](https://owl.purdue.edu/owl/general_writing/mechanics/help_with_capitals.html)。 +3. 在说明中用"you"代替"we"。 +4. 使用现在时态,避免使用`will`。 +5. 尽量使用主动语态而不是被动。 + +此外,为了使文件一致: + +1. 句子不宜过长, e.g., 长度<=80 +2. 尽量使用相对路径,假设我们在 repo 的根目录下,例如,`doc-site/docs`指的 + 是`singa-doc/docs-site/docs`。 +3. 使用背标将命令、路径、类函数和变量亮出来,例如,`Tensor`, + `singa-doc/docs-site/docs`。 +4. 为了突出其他术语/概念,使用 _斜体_ or **加粗** + +本项目使用的[prettier tool](https://prettier.io/)会在我们进行 git 提交时,根 +据[配置](https://github.com/apache/singa-doc/blob/master/docs-site/.prettierrc)自 +动格式化代码。例如,它会将 markdown 文件中的文本最多折叠成 80 个字符(注释行除外 +)。 + +在介绍一个概念(如`Tensor`类)时,要提供概述(目的和与其他概念的关系)、API 和例 +子,还可以用 Google colab 来演示其用法。 + +详细的编辑 md 文件和建立网站的方法请参 +考[本页面](https://github.com/apache/singa-doc/tree/master/docs-site)。 + +## API References + +### CPP API + +请遵 +循[Google CPP 注释风格](https://google.github.io/styleguide/cppguide.html#Comments). + +要生成文档,请从 doc 文件夹中运行 "doxygen"(推荐使用 Doxygen >= 1.8)。 + +### Python API + +请遵 +循[Google Python DocString 风格](http://google.github.io/styleguide/pyguide.html#38-comments-and-docstrings). + +## Visual Studio Code (vscode) + +如果你使用 vscode 作为编辑器,我们推荐使用以下插件。 + +### Docstring Snippet + +[autoDocstring](https://marketplace.visualstudio.com/items?itemName=njpwerner.autodocstring)生 +成函数、类等的 docstring,要注意选择使用`google`的 docString 格式。 + +### Spell Check + +[Code Spell Checker](https://marketplace.visualstudio.com/items?itemName=streetsidesoftware.code-spell-checker)可 +以用来检查代码的注释,或`.md`和`.rst`文件。 + +要只对 Python 代码的注释进行拼写检查,可以 +在`File - Preferences - User Snippets - python.json`中添加以下代码段: + + "cspell check" : { + "prefix": "cspell", + "body": [ + "# Directives for doing spell check only for python and c/cpp comments", + "# cSpell:includeRegExp #.* ", + "# cSpell:includeRegExp (\"\"\"|''')[^\1]*\1", + "# cSpell: CStyleComment", + ], + "description": "# spell check only for python comments" + } + +如果要只对 Cpp 代码的注释进行拼写检查,可以 +在`File - Preferences - User Snippets - cpp.json`中添加以下代码段: + + "cspell check" : { + "prefix": "cspell", + "body": [ + "// Directive for doing spell check only for cpp comments", + "// cSpell:includeRegExp CStyleComment", + ], + "description": "# spell check only for cpp comments" + } diff --git a/docs-site/website/versioned_docs/version-4.3.0_Chinese/device.md b/docs-site/website/versioned_docs/version-4.3.0_Chinese/device.md new file mode 100644 index 0000000..94d0460 --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0_Chinese/device.md @@ -0,0 +1,31 @@ +--- +id: version-4.2.0_Chinese-device +title: Device +original_id: device +--- + + + +Device 代表一个有内存和计算单元的硬件设备。所有的[Tensor 操作](./tensor)都由常驻 +设备安排执行的,Tensor 内存也由设备的内存管理器管理。因此,需要在 Device 类中实 +现内存和执行的优化。 + +## 特定设备 + +目前,SINGA 支持三种设备: + +1. CudaGPU:用于运行 Cuda 代码的 Nvidia GPU。 +2. CppCPU:用于运行 Cpp 代码的 CPU。 +3. OpenclGPU:用于运行 OpenCL 代码的 GPU 卡。 + +## 用法示例 + +下面的代码提供了创建设备的例子: + +```python +from singa import device +cuda = device.create_cuda_gpu_on(0) # use GPU card of ID 0 +host = device.get_default_device() # get the default host device (a CppCPU) +ary1 = device.create_cuda_gpus(2) # create 2 devices, starting from ID 0 +ary2 = device.create_cuda_gpus([0,2]) # create 2 devices on ID 0 and 2 +``` diff --git a/docs-site/website/versioned_docs/version-4.3.0_Chinese/dist-train.md b/docs-site/website/versioned_docs/version-4.3.0_Chinese/dist-train.md new file mode 100644 index 0000000..45e2fa1 --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0_Chinese/dist-train.md @@ -0,0 +1,423 @@ +--- +id: version-4.2.0_Chinese-dist-train +title: Distributed Training +original_id: dist-train +--- + + + +SINGA 支持跨多个 GPU 的数据并行训练(在单个节点上或跨不同节点)。下图说明了数据 +并行训练的情况: + +![MPI.png](assets/MPI.png) + +在分布式训练中,每个进程(称为 worker)在单个 GPU 上运行一个训练脚本,每个进程都有 +一个单独的通信等级,训练数据被分发给各个 worker,模型在每个 worker 上被复制。在 +每次迭代中,worker 从其分区中读取数据的一个 mini-batch(例如,256 张图像),并运 +行 BackPropagation 算法来计算权重的梯度,通过 all-reduce( +由[NCCL](https://developer.nvidia.com/nccl)提供)进行平均,按照随机梯度下降算法 +(SGD)进行权重更新。 + +NCCL 的 all-reduce 操作可以用来减少和同步不同 GPU 的梯度。假设我们使用 4 个 GPU +进行训练,如下图所示。一旦计算出 4 个 GPU 的梯度,all-reduce 将返回 GPU 的梯度之 +和,并使其在每个 GPU 上可用,然后就可以轻松计算出平均梯度。 + +![AllReduce.png](assets/AllReduce.png) + +## 使用 + +SINGA 提供了一个名为`DistOpt`(`Opt`的一个子类)的模块,用于分布式训练。它封装了 +一个普通的 SGD 优化器,并调用`Communicator`进行梯度同步。下面的例子说明了如何使 +用`DistOpt`在 MNIST 数据集上训练一个 CNN 模型。源代码 +在[这里](https://github.com/apache/singa/blob/master/examples/cnn/),或者可以使 +用[Colab notebook]()。 + +### 代码示例 + +1. 定义神经网络模型: + +```python +class CNN(model.Model): + + def __init__(self, num_classes=10, num_channels=1): + super(CNN, self).__init__() + self.conv1 = layer.Conv2d(num_channels, 20, 5, padding=0, activation="RELU") + self.conv2 = layer.Conv2d(20, 50, 5, padding=0, activation="RELU") + self.linear1 = layer.Linear(500) + self.linear2 = layer.Linear(num_classes) + self.pooling1 = layer.MaxPool2d(2, 2, padding=0) + self.pooling2 = layer.MaxPool2d(2, 2, padding=0) + self.relu = layer.ReLU() + self.flatten = layer.Flatten() + self.softmax_cross_entropy = layer.SoftMaxCrossEntropy() + + def forward(self, x): + y = self.conv1(x) + y = self.pooling1(y) + y = self.conv2(y) + y = self.pooling2(y) + y = self.flatten(y) + y = self.linear1(y) + y = self.relu(y) + y = self.linear2(y) + return y + + def train_one_batch(self, x, y, dist_option='fp32', spars=0): + out = self.forward(x) + loss = self.softmax_cross_entropy(out, y) + + # Allow different options for distributed training + # See the section "Optimizations for Distributed Training" + if dist_option == 'fp32': + self.optimizer(loss) + elif dist_option == 'fp16': + 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 + +# create model +model = CNN() +``` + +2. 创建`DistOpt`实例并将其应用到创建的模型上: + +```python +sgd = opt.SGD(lr=0.005, momentum=0.9, weight_decay=1e-5) +sgd = opt.DistOpt(sgd) +model.set_optimizer(sgd) +dev = device.create_cuda_gpu_on(sgd.local_rank) +``` + +下面是关于代码中一些变量的解释: + +(i) `dev` + +dev 代表`Device`实例,在设备中加载数据并运行 CNN 模型。 + +(ii)`local_rank` + +Local rank 表示当前进程在同一节点中使用的 GPU 数量。例如,如果你使用的节点有 2 +个 GPU,`local_rank=0`表示这个进程使用的是第一个 GPU,而`local_rank=1`表示使用的 +是第二个 GPU。使用 MPI 或多进程,你能够运行相同的训练脚本,唯一的区 +别`local_rank`的值不同。 + +(iii)`global_rank` + +global 中的 rank 代表了你使用的所有节点中所有进程的全局排名。让我们考虑这样的情 +况:你有 3 个节点,每个节点有两个 GPU, `global_rank=0`表示使用第 1 个节点的第 1 +个 GPU 的进程, `global_rank=2`表示使用第 2 个节点的第 1 个 GPU 的进程, +`global_rank=4`表示使用第 3 个节点的第 1 个 GPU 的进程。 + +3. 加载和分割训练/验证数据: + +```python +def data_partition(dataset_x, dataset_y, global_rank, world_size): + data_per_rank = dataset_x.shape[0] // world_size + idx_start = global_rank * data_per_rank + idx_end = (global_rank + 1) * data_per_rank + return dataset_x[idx_start:idx_end], dataset_y[idx_start:idx_end] + +train_x, train_y, test_x, test_y = load_dataset() +train_x, train_y = data_partition(train_x, train_y, + sgd.global_rank, sgd.world_size) +test_x, test_y = data_partition(test_x, test_y, + sgd.global_rank, sgd.world_size) +``` + +这个`dev`的数据集的一个分区被返回。 + +这里,`world_size`代表你用于分布式训练的所有节点中的进程总数。 + +4. 初始化并同步所有 worker 的模型参数: + +```python +#Synchronize the initial parameter +tx = tensor.Tensor((batch_size, 1, IMG_SIZE, IMG_SIZE), dev, tensor.float32) +ty = tensor.Tensor((batch_size, num_classes), dev, tensor.int32) +model.compile([tx], is_train=True, use_graph=graph, sequential=True) +... +#Use the same random seed for different ranks +seed = 0 +dev.SetRandSeed(seed) +np.random.seed(seed) +``` + +5. 运行 BackPropagation 和分布式 SGD + +```python +for epoch in range(max_epoch): + for b in range(num_train_batch): + x = train_x[idx[b * batch_size: (b + 1) * batch_size]] + y = train_y[idx[b * batch_size: (b + 1) * batch_size]] + tx.copy_from_numpy(x) + ty.copy_from_numpy(y) + # Train the model + out, loss = model(tx, ty) +``` + +### 执行命令 + +有两种方式可以启动训练,MPI 或 Python multiprocessing。 + +#### Python multiprocessing + +它可以在一个节点上使用多个 GPU,其中,每个 GPU 是一个 worker。 + +1. 将上述训练用的代码打包进一个函数: + +```python +def train_mnist_cnn(nccl_id=None, local_rank=None, world_size=None): + ... +``` + +2. 创建`mnist_multiprocess.py`。 + +```python +if __name__ == '__main__': + # Generate a NCCL ID to be used for collective communication + nccl_id = singa.NcclIdHolder() + + # Define the number of GPUs to be used in the training process + world_size = int(sys.argv[1]) + + # Define and launch the multi-processing + import multiprocessing + process = [] + for local_rank in range(0, world_size): + process.append(multiprocessing.Process(target=train_mnist_cnn, + args=(nccl_id, local_rank, world_size))) + + for p in process: + p.start() +``` + +下面是关于上面所创建的变量的一些说明: + +(i) `nccl_id` + +需要注意的是,我们在这里需要生成一个 NCCL ID,用于集体通信,然后将其传递给所有进 +程。NCCL ID 就像一个门票,只有拥有这个 ID 的进程才能加入到 all-reduce 操作中。( +如果我们接下来使用 MPI,NCCL ID 的传递就没有必要了,因为在我们的代码中,这个 ID +会由 MPI 自动广播。) + +(ii) `world_size` + +world_size 是您想用于训练的 GPU 数量。 + +(iii) `local_rank` + +local_rank 决定分布式训练的本地顺序,以及在训练过程中使用哪个 gpu。在上面的代码 +中,我们使用 for 循环来运行训练函数,其中参数 local_rank 从 0 迭代到 +world_size。在这种情况下,不同的进程可以使用不同的 GPU 进行训练。 + +创建`DistOpt`实例的参数应按照如下方式更新: + +```python +sgd = opt.DistOpt(sgd, nccl_id=nccl_id, local_rank=local_rank, world_size=world_size) +``` + +3. 运行`mnist_multiprocess.py`: + +```sh +python mnist_multiprocess.py 2 +``` + +与单 GPU 训练相比,它最主要的意义是速度提升: + +``` +Starting Epoch 0: +Training loss = 408.909790, training accuracy = 0.880475 +Evaluation accuracy = 0.956430 +Starting Epoch 1: +Training loss = 102.396790, training accuracy = 0.967415 +Evaluation accuracy = 0.977564 +Starting Epoch 2: +Training loss = 69.217010, training accuracy = 0.977915 +Evaluation accuracy = 0.981370 +Starting Epoch 3: +Training loss = 54.248390, training accuracy = 0.982823 +Evaluation accuracy = 0.984075 +Starting Epoch 4: +Training loss = 45.213406, training accuracy = 0.985560 +Evaluation accuracy = 0.985276 +Starting Epoch 5: +Training loss = 38.868435, training accuracy = 0.987764 +Evaluation accuracy = 0.986278 +Starting Epoch 6: +Training loss = 34.078186, training accuracy = 0.989149 +Evaluation accuracy = 0.987881 +Starting Epoch 7: +Training loss = 30.138697, training accuracy = 0.990451 +Evaluation accuracy = 0.988181 +Starting Epoch 8: +Training loss = 26.854443, training accuracy = 0.991520 +Evaluation accuracy = 0.988682 +Starting Epoch 9: +Training loss = 24.039650, training accuracy = 0.992405 +Evaluation accuracy = 0.989083 +``` + +#### MPI + +只要有多个 GPU,MPI 既适用于单节点,也适用于多节点。 + +1. 创建`mnist_dist.py`。 + +```python +if __name__ == '__main__': + train_mnist_cnn() +``` + +2. 为 MPI 生成一个 hostfile,例如下面的 hostfile 在一个节点上使用了 2 个进程(即 + 2 个 GPU): + +```txt +localhost:2 +``` + +3. 通过`mpiexec`启动训练: + +```sh +mpiexec --hostfile host_file python mnist_dist.py +``` + +与单 GPU 训练相比,它同样可以带来速度的提升: + +``` +Starting Epoch 0: +Training loss = 383.969543, training accuracy = 0.886402 +Evaluation accuracy = 0.954327 +Starting Epoch 1: +Training loss = 97.531479, training accuracy = 0.969451 +Evaluation accuracy = 0.977163 +Starting Epoch 2: +Training loss = 67.166870, training accuracy = 0.978516 +Evaluation accuracy = 0.980769 +Starting Epoch 3: +Training loss = 53.369656, training accuracy = 0.983040 +Evaluation accuracy = 0.983974 +Starting Epoch 4: +Training loss = 45.100403, training accuracy = 0.985777 +Evaluation accuracy = 0.986078 +Starting Epoch 5: +Training loss = 39.330826, training accuracy = 0.987447 +Evaluation accuracy = 0.987179 +Starting Epoch 6: +Training loss = 34.655270, training accuracy = 0.988799 +Evaluation accuracy = 0.987780 +Starting Epoch 7: +Training loss = 30.749735, training accuracy = 0.989984 +Evaluation accuracy = 0.988281 +Starting Epoch 8: +Training loss = 27.422146, training accuracy = 0.991319 +Evaluation accuracy = 0.988582 +Starting Epoch 9: +Training loss = 24.548153, training accuracy = 0.992171 +Evaluation accuracy = 0.988682 +``` + +## 针对分布式训练的优化 + +SINGA 为分布式训练提供了多种优化策略,以降低模块间的通信成本。参考`DistOpt`的 +API,了解每种策略的配置。 + +当我们使用`model.Model`建立模型时,我们需要在`training_one_batch`方法中启用分布 +式训练的选项,请参考本页顶部的示例代码。我们也可以直接复制这些选项的代码,然后在 +其他模型中使用。 + +有了定义的选项,我们可以在使用`model(tx, ty, dist_option, spars)`开始训练时,设 +置对应的参数`dist_option`和`spars`。 + +### 不采取优化手段 + +```python +out, loss = model(tx, ty) +``` + +`loss`是损失函数的输出张量,例如分类任务中的交叉熵。 + +### 半精度梯度(Half-precision Gradients) + +```python +out, loss = model(tx, ty, dist_option = 'fp16') +``` + +在调用 all-reduce 之前,它将每个梯度值转换为 16-bit 表示(即半精度)。 + +### 部分同步(Partial Synchronization) + +```python +out, loss = model(tx, ty, dist_option = 'partialUpdate') +``` + +在每一次迭代中,每个 rank 都做本地 SGD 更新。然后,只对一个部分的参数进行平均同 +步,从而节省了通信成本。分块大小是在创建`DistOpt`实例时配置的。 + +### 梯度稀疏化(Gradient Sparsification) + +该策略应用稀疏化方案来选择梯度的子集进行 all-reduce,有两种方式: + +- 选择前 k 大的元素,spars 是被选择的元素的一部分(比例在 0 - 1 之间)。 + +```python +out, loss = model(tx, ty, dist_option = 'sparseTopK', spars = spars) +``` + +- 所有绝对值大于预定义阈值的梯度都会被选中。 + +```python +out, loss = model(tx, ty, dist_option = 'sparseThreshold', spars = spars) +``` + +超参数在创建`DistOpt`实例时配置。 + +## 实现 + +本节主要是让开发者了解分布训练模块的代码是如何实现的。 + +### NCCL communicator 的 C 接口 + +首先,通信层是用 C 语 +言[communicator.cc](https://github.com/apache/singa/blob/master/src/io/communicator.cc)编 +写的,它调用用 NCCL 库进行集体通信。 + +communicator 有两个构造器,一个是 MPI 的,另一个是 Multiprocess 的。 + +(i) MPI 构造器 + +构造器首先先获取全局 rank 和 world_size,计算出本地 rank,然后由 rank 0 生成 +NCCL ID 并广播给每个 rank。之后,它调用 setup 函数来初始化 NCCL +communicator、cuda 流和缓冲区。 + +(ii) Python multiprocess 构造器 + +构造器首先从输入参数中获取 rank、world_size 和 NCCL ID。之后,调用 setup 函数来 +初始化 NCCL communicator、cuda 流和缓冲区。 + +在初始化之后,它提供了 all-reduce 功能来同步模型参数或梯度。例如,synch 接收一个 +输入张量,通过 NCCL 例程进行 all-reduce,在我们调用 synch 之后,需要调用 wait 函 +数来等待 all-reduce 操作的完成。 + +### DistOpt 的 Python 接口 + +然后,python 接口提供了一 +个[DistOpt](https://github.com/apache/singa/blob/master/python/singa/opt.py)类来 +封装一 +个[optimizer](https://github.com/apache/singa/blob/master/python/singa/opt.py)对 +象,以执行基于 MPI 或 Multiprocess 的分布式训练。在初始化过程中,它创建了一个 +NCCL communicator 对象(来自于上面小节提到的 C 接口),然后,`DistOpt`中的每一次 +all-reduce 操作都会用到这个 communicator 对象。 + +在 MPI 或 Multiprocess 中,每个进程都有一个独立的 rank,它给出了各个进程使用的 +GPU 的信息。训练数据是被拆分的,因此每个进程可以根据一部分训练数据来评估子梯度。 +一旦每个进程的子梯度被计算出来,就可以将所有进程计算出的子梯度做 all-reduce,得 +到总体随机梯度。 diff --git a/docs-site/website/versioned_docs/version-4.3.0_Chinese/download.md b/docs-site/website/versioned_docs/version-4.3.0_Chinese/download.md new file mode 100644 index 0000000..f254ead --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0_Chinese/download.md @@ -0,0 +1,162 @@ +--- +id: version-4.2.0_Chinese-downloads +title: Download SINGA +original_id: downloads +--- + + + +## Verify + +要验证下载的 tar.gz 文件,下载[KEYS](https://www.apache.org/dist/singa/KEYS)和 +ASC 文件,然后执行以下命令: + +```shell +% gpg --import KEYS +% gpg --verify downloaded_file.asc downloaded_file +``` + +你也可以检查 SHA512 或 MD5 值判断下载是否完成。 + +## V3.0.0 (18 April 2020): + +- [Apache SINGA 3.0.0](http://www.apache.org/dyn/closer.cgi/singa/3.0.0/apache-singa-3.0.0.tar.gz) + [\[SHA512\]](https://www.apache.org/dist/singa/3.0.0/apache-singa-3.0.0.tar.gz.sha512) + [\[ASC\]](https://www.apache.org/dist/singa/3.0.0/apache-singa-3.0.0.tar.gz.asc) +- [Release Notes 3.0.0](releases/RELEASE_NOTES_3.0.0) +- 新特性及重要更新: + - 增强了 ONNX。在 SINGA 中测试了多种 ONNX 模型。 + - 使用 MPI 和 NCCL Communication 进行分布式训练,通过梯度稀疏化和压缩以及分块 + 传输进行了优化。 + - 计算图的构建,利用图优化了速度和内存。 + - 新的文档网站(singa.apache.org)和 API 参考网站(apache-singa.rtfd.io)。 + - 使用 CI 实现代码质量检查。 + - 将 MKLDNN 替换为 DNNL。 + - 更新 Tensor API 以支持广播操作。 + - 实现了支持 ONNX 模型的新的 autograd 操作符。 + +## 孵化版本(incubating) v2.0.0 (20 April 2019): + +- [Apache SINGA 2.0.0 (incubating)](http://www.apache.org/dyn/closer.cgi/incubator/singa/2.0.0/apache-singa-incubating-2.0.0.tar.gz) + [\[SHA512\]](https://www.apache.org/dist/incubator/singa/2.0.0/apache-singa-incubating-2.0.0.tar.gz.sha512) + [\[ASC\]](https://www.apache.org/dist/incubator/singa/2.0.0/apache-singa-incubating-2.0.0.tar.gz.asc) +- [Release Notes 2.0.0 (incubating)](releases/RELEASE_NOTES_2.0.0.html) +- 新特性及重要更新: + - 增强了 autograd 功能(适用于卷积网络和循环网络)。 + - 支持 ONNX。 + - 通过英特尔 MKL DNN lib 改进 CPP 操作。 + - 实现 tensor 广播。 + - 在 Apache 用户名下移动 Docker 镜像。 + - 更新 conda-build 配置中依赖的 lib 版本。 + +## 孵化版本(incubating) v1.2.0 (6 June 2018): + +- [Apache SINGA 1.2.0 (incubating)](https://archive.apache.org/dist/incubator/singa/1.2.0/apache-singa-incubating-1.2.0.tar.gz) + [\[SHA512\]](https://archive.apache.org/dist/incubator/singa/1.2.0/apache-singa-incubating-1.2.0.tar.gz.sha512) + [\[ASC\]](https://archive.apache.org/dist/incubator/singa/1.2.0/apache-singa-incubating-1.2.0.tar.gz.asc) +- [Release Notes 1.2.0 (incubating)](releases/RELEASE_NOTES_1.2.0.html) +- 新特性及重要更新: + - 实现了 autograd(目前支持 MLP 模式)。 + - 升级 PySinga 以支持 Python 3 + - 改进 Tensor 类的 stride 范围。 + - 将 cuDNN 从 V5 升级到 V7。 + - 增加 VGG、Inception V4、ResNet 和 DenseNet 进行 ImageNet 分类。 + - 为 conda 包创建别名 + - 完整的中文文档 + - 添加在 Windows 上运行 Singa 的说明 + - 更新编译,CI + - 修复一些错误 + +## 孵化版本(incubating) v1.1.0 (12 February 2017): + +- [Apache SINGA 1.1.0 (incubating)](https://archive.apache.org/dist/incubator/singa/1.1.0/apache-singa-incubating-1.1.0.tar.gz) + [\[MD5\]](https://archive.apache.org/dist/incubator/singa/1.1.0/apache-singa-incubating-1.1.0.tar.gz.md5) + [\[ASC\]](https://archive.apache.org/dist/incubator/singa/1.1.0/apache-singa-incubating-1.1.0.tar.gz.asc) +- [Release Notes 1.1.0 (incubating)](releases/RELEASE_NOTES_1.1.0.html) +- 新特性和重要更新: + - 创建 Docker 镜像(CPU 和 GPU 版本) + - 为 SINGA 创建 Amazon AMI(CPU 版)。 + - 集成 Jenkins,自动生成 Wheel 和 Debian 包(用于安装),并更新网站。 + - 增强 FeedFowardNet,例如,多输入和用于调试的 verbose 模式。 + - 添加 Concat 和 Slice 层。 + - 优化 CrossEntropyLoss 以接受带有多个标签的实例。 + - 添加包含图像增强方法的 image_tool.py。 + - 支持通过快照 API 加载和保存模型。 + - 在 Windows 上编译 SINGA 源代码。 + - 将必要依赖库与 SINGA 代码一起编译。 + - 为 SINGA 启用 Java binding(基本)。 + - 在 checkpoint 文件中添加版本 ID。 + - 增加 Rafiki 工具包以提供 RESTFul APIs。 + - 添加了从 Caffe 预训练的例子,包括 GoogleNet。 + +## 孵化版本(incubating) v1.0.0 (8 September 2016): + +- [Apache SINGA 1.0.0 (incubating)](https://archive.apache.org/dist/incubator/singa/1.0.0/apache-singa-incubating-1.0.0.tar.gz) + [\[MD5\]](https://archive.apache.org/dist/incubator/singa/1.0.0/apache-singa-incubating-1.0.0.tar.gz.md5) + [\[ASC\]](https://archive.apache.org/dist/incubator/singa/1.0.0/apache-singa-incubating-1.0.0.tar.gz.asc) +- [Release Notes 1.0.0 (incubating)](releases/RELEASE_NOTES_1.0.0.html) +- 新特性和重要更新: + - 创建 Tensor 概念,用于支持更多的机器学习模型。 + - 创建 Device 概念,用于运行在不同的硬件设备上,包括 CPU,(Nvidia/AMD) GPU 和 + FPGA (将在以后的版本中测试)。 + - 用 cmake 取代 GNU autotool 进行编译。 + - 支持 Mac OS。 + - 改进 Python binding,包括安装和编程。 + - 更多的深度学习模型,包括 VGG 和 ResNet。 + - 更多的 IO 类用于读取/写入文件和编码/解码数据。 + - 直接基于 Socket 的新网络通信组件。 + - Cudnn V5,包含 Dropout 和 RNN 层。 + - 将网页构建工具从 maven 更换为 Sphinx。 + - 整合 Travis-CI。 + +## 孵化版本(incubating) v0.3.0 (20 April 2016): + +- [Apache SINGA 0.3.0 (incubating)](https://archive.apache.org/dist/incubator/singa/0.3.0/apache-singa-incubating-0.3.0.tar.gz) + [\[MD5\]](https://archive.apache.org/dist/incubator/singa/0.3.0/apache-singa-incubating-0.3.0.tar.gz.md5) + [\[ASC\]](https://archive.apache.org/dist/incubator/singa/0.3.0/apache-singa-incubating-0.3.0.tar.gz.asc) +- [Release Notes 0.3.0 (incubating)](releases/RELEASE_NOTES_0.3.0.html) +- 新特性和重要更新: + - 在 GPU 集群上进行训练,可以在 GPU 集群上进行深度学习模型的训练。 + - 改进 Python wrapper 简化配置工作,包括神经网络和 SGD 算法。 + - 新增 SGD 更新器,包括 Adam、AdaDelta 和 AdaMax。 + - 安装时减少了单节点训练的依赖库。 + - 使用 CPU 和 GPU 进行异构训练。 + - 支持 cuDNN V4。 + - 数据预取。 + - 修复一些 bug。 + +## 孵化版本(incubating) v0.2.0 (14 January 2016): + +- [Apache SINGA 0.2.0 (incubating)](https://archive.apache.org/dist/incubator/singa/0.2.0/apache-singa-incubating-0.2.0.tar.gz) + [\[MD5\]](https://archive.apache.org/dist/incubator/singa/0.2.0/apache-singa-incubating-0.2.0.tar.gz.md5) + [\[ASC\]](https://archive.apache.org/dist/incubator/singa/0.2.0/apache-singa-incubating-0.2.0.tar.gz.asc) +- [Release Notes 0.2.0 (incubating)](releases/RELEASE_NOTES_0.2.0.html) +- 新特性和重要更新: + - 在 GPU 上进行训练,可以在一个节点上用多个 GPU 卡训练复杂的模型。 + - 混合神经网分区支持数据和模型同时并行。 + - Python wrapper 简化配置,包括神经网络和 SGD 算法。 + - 实现了 RNN 模型和 BPTT 算法,支持基于 RNN 模型的应用,如 GRU。 + - 云软件集成,包括 Mesos、Docker 和 HDFS。 + - 可视化神经网结构和层信息,以便优化调试。 + - 针对 Blobs 和原始数据指针的线性代数函数和随机函数。 + - 添加新的层,包括 SoftmaxLayer、ArgSortLayer、DummyLayer、RNN 层和 cuDNN 层。 + - 更新 Layer 类以携带多个数据/梯度 Blobs。 + - 通过加载之前训练的模型参数,提取特征并测试新数据的性能。 + - 为 IO 操作添加 Store 类。 + +## Incubating v0.1.0 (8 October 2015): + +- [Apache SINGA 0.1.0 (incubating)](https://archive.apache.org/dist/incubator/singa/apache-singa-incubating-0.1.0.tar.gz) + [\[MD5\]](https://archive.apache.org/dist/incubator/singa/apache-singa-incubating-0.1.0.tar.gz.md5) + [\[ASC\]](https://archive.apache.org/dist/incubator/singa/apache-singa-incubating-0.1.0.tar.gz.asc) +- [Amazon EC2 image](https://console.aws.amazon.com/ec2/v2/home?region=ap-southeast-1#LaunchInstanceWizard:ami=ami-b41001e6) +- [Release Notes 0.1.0 (incubating)](releases/RELEASE_NOTES_0.1.0.html) +- 新特性和重要更新: + - 允许使用 GNU 构建工具进行安装。 + - 实现用 zookeeper 进行工作管理的脚本。 + - 实现基于 NeuralNet 和 Layer 的编程模型。 + - 实现基于 Worker、Server 和 Stub 的系统架构。 + - 训练模型来自三种不同的模型类别,即前馈模型、能量模型和 RNN 模型。 + - 使用 CPU 的同步和异步分布式训练框架。 + - checkpoint 文件生成和恢复。 + - 使用 gtest 进行单元测试。 diff --git a/docs-site/website/versioned_docs/version-4.3.0_Chinese/examples.md b/docs-site/website/versioned_docs/version-4.3.0_Chinese/examples.md new file mode 100644 index 0000000..745623f --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0_Chinese/examples.md @@ -0,0 +1,67 @@ +--- +id: version-4.2.0_Chinese-examples +title: Examples +original_id: examples +--- + + + +本页列出了一些使用 SINGA 的深度学习任务示例。源代码维护在 +[Github](https://github.com/apache/singa/tree/master/examples) 上的 SINGA repo +内。对于使用 SINGA Python API 在 CPU 或单 GPU 上运行的例子,它们也可以 +在[Google Colab](https://colab.research.google.com/)上获得。你可以直接在谷歌云上 +运行它们,而无需在本地设置环境。下面给出了每个例子的链接。 + +## 图像分类 + +| 网络模型 | 数据集 | 链接 | +| ----------- | --------------------------------- | ------------------------------------------------------------------------------------------------------- | +| Simple CNN | MNIST, CIFAR10, CIFAR100 | [Colab](https://colab.research.google.com/drive/1fbGUs1AsoX6bU5F745RwQpohP4bHTktq) | +| AlexNet | ImageNet | [Cpp]() | +| VGG | ImageNet | [Cpp](), [Python](), [Colab](https://colab.research.google.com/drive/14kxgRKtbjPCKKsDJVNi3AvTev81Gp_Ds) | +| XceptionNet | MNIST, CIFAR10, CIFAR100 | [Python]() | +| ResNet | MNIST, CIFAR10, CIFAR100, CIFAR10 | [Python](), [Colab](https://colab.research.google.com/drive/1u1RYefSsVbiP4I-5wiBKHjsT9L0FxLm9) | +| MobileNet | ImageNet | [Colab](https://colab.research.google.com/drive/1HsixqJMIpKyEPhkbB8jy7NwNEFEAUWAf) | + +## 目标检测 + +| 网络模型 | 数据集 | 链接 | +| ----------- | ---------- | ---------------------------------------------------------------------------------- | +| Tiny YOLOv2 | Pascal VOC | [Colab](https://colab.research.google.com/drive/11V4I6cRjIJNUv5ZGsEGwqHuoQEie6b1T) | + +## 面部及表情识别 + +| 模型 | 数据集 | 链接 | +| --------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------ | ---------------------------------------------------------------------------------- | +| ArcFace | Refined MS-Celeb-1M | [Colab](https://colab.research.google.com/drive/1qanaqUKGIDtifdzEzJOHjEj4kYzA9uJC) | +| Emotion FerPlus | [Facial Expression Recognition Challenge](https://www.kaggle.com/c/challenges-in-representation-learning-facial-expression-recognition-challenge/data) | [Colab](https://colab.research.google.com/drive/1XHtBQGRhe58PDi4LGYJzYueWBeWbO23r) | + +## 图像生成 + +| 模型 | 数据集 | 链接 | +| ----- | ------ | ---------------------------------------------------------------------------------- | +| GAN | MNIST | [Colab](https://colab.research.google.com/drive/1f86MNDW47DJqHoIqWD1tOxcyx2MWys8L) | +| LSGAN | MNIST | [Colab](https://colab.research.google.com/drive/1C6jNRf28vnFOI9JVM4lpkJPqxsnhxdol) | + +## 机器理解 + +| 模型 | 数据集 | 链接 | +| ---------- | ------------------------------------------------------------------------- | ---------------------------------------------------------------------------------- | +| Bert-Squad | [SQuAD v1.1](https://rajpurkar.github.io/SQuAD-explorer/explore/1.1/dev/) | [Colab](https://colab.research.google.com/drive/1kud-lUPjS_u-TkDAzihBTw0Vqr0FjCE-) | + +## 文字识别 + +| 模型 | 数据集 | 链接 | +| ----------- | ------ | ---------- | +| Simple LSTM | IMDB | [python]() | + +## 文本排序 + +| 模型 | 数据集 | 链接 | +| ------ | ----------- | ---------- | +| BiLSTM | InsuranceQA | [python]() | + +## 其他 + +- MNIST 数据集的有限玻尔兹曼机, [source](), + [Colab](https://colab.research.google.com/drive/19996noGu9JyHHkVmp4edBGu7PJSRQKsd). diff --git a/docs-site/website/versioned_docs/version-4.3.0_Chinese/git-workflow.md b/docs-site/website/versioned_docs/version-4.3.0_Chinese/git-workflow.md new file mode 100644 index 0000000..99e473a --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0_Chinese/git-workflow.md @@ -0,0 +1,121 @@ +--- +id: version-4.2.0_Chinese-git-workflow +title: Git Workflow +original_id: git-workflow +--- + + + +## 对于开发者 + +1. 将[SINGA Github repository](https://github.com/apache/singa) fork 到你自己的 + Github 账户。 + +2. 从你自己的 git 仓库中 clone **repo** (short for repository): + + ```shell + git clone https://github.com//singa.git + git remote add upstream https://github.com/apache/singa.git + ``` + +3. 创建一个新的分支(例如 `feature-foo` 或 `fixbug-foo`),在这个分支上工作并提 + 交你的代码: + + ```shell + git checkout dev + git checkout -b feature-foo + # write your code + git add + git commit + ``` + + commit 信息应包括: + + - 一个概括性的标题。 + - 详细的描述。如果提交是为了修复 bug,描述中最好包括问题的简短复现;如果是新 + 功能,可以描述新功能的动机/目的。 + + 如果您的分支有很多小的 commit,您需要通过: + + ```shell + git rebase -i + ``` + + 你可以[压制和重写](https://help.github.com/en/articles/about-git-rebase)提交 + 的内容。 + +4. 当你在写代码的时候,SINGA 的`dev`分支可能已经被别人更新了;在这种情况下,你需 + 要拉取最新的`dev`分支: + + ```shell + git checkout dev + git pull upstream dev:dev + ``` + +5. 将`feature-foo` + [rebase](https://git-scm.com/book/en/v2/Git-Branching-Rebasing)到`dev`分支上 + ,并将提交的内容推送到自己的 Github 账户(你刚刚创建的新分支),rebase 操作是 + 为了清理提交历史。提交当前工作后,应执行以下 git 指令: + + ```shell + git checkout feature-foo + git rebase dev + git push origin feature-foo:feature-foo + ``` + + Rebase 命令 + 的[操作步骤](https://git-scm.com/book/en/v2/Git-Branching-Rebasing)如下: "这 + 个操作的工作原理是进入到两个分支(你所在的分支和你要 rebase 的分支)的共同来 + 源 -> 获取你所在分支的每次 commit 所引入的差异 -> 将这些差异保存到临时文件中 + -> 将当前分支重置为与你要 rebase 的分支相同的 commit -> 最后依次修改每个差异 + 。" + + 因此,执行后,你还是在特性分支上,但你自己的提交 ID/hash 被改变了,因为 diffs + 是在 rebase 时提交的;而且你的分支现在有来自`dev`分支和你自己分支的最新代码。 + +6. 在 Github 网站上创建一个针对 apache/singa `dev`分支的 pull request(PR)。如 + 果你想通知其他在相同文件上工作过的贡献者,你可以在 Github 上找到文件,然后点 + 击 "Blame",就可以看到最后修改代码的人的逐行注释。然后,你可以在 PR 描述中加 + 上@username,就可以立即 ping 到他们。请说明该贡献是你的原创作品,并且你在项目 + 的开源许可下将该作品授权给项目。你的新分支的进一步提交(例如,bug 修复)将由 + Github 自动添加到这个 pull request 中。 + +7. 接下来等待 committer 审核 PR。在这段时间里,SINGA 的`dev`可能已经被其他人更新 + 了,这时你需 + 要[合并](https://docs.fast.ai/dev/git.html#how-to-keep-your-feature-branch-up-to-date)最 + 新的`dev`来解决冲突。有些人将 PR 重 + 新[rebase 到最新的 dev](https://github.com/edx/edx-platform/wiki/How-to-Rebase-a-Pull-Request)上 + ,而不是合并。但是,如果其他开发者获取这个 PR 来添加新的功能,然后再发送 PR, + 那么 rebase 操作会在未来的 PR 中引入**重复的提交**(不同的哈希)。关于何时避 + 免使用 rebase 的细节,请参 + 见[The Golden Rule of Rebasing](https://www.atlassian.com/git/tutorials/merging-vs-rebasing)。 + 另一种简单的更新 PR 的方法(修复冲突或提交错误)是,从 Apache SINGAS repo 的 + 最新开发分支中 checkout 出一个新的分支,复制并粘贴更新/添加的代码,然后 + commit 并发送一个新的 PR。 + +## 对于 Committers + +Committer 可以将 PR 合并到上游 repo 的 dev 分支。在合并每一个 PR 之前,提交者应 +该做到: + +- 检查 commit 信息(内容和格式) +- 检查对现有代码的修改,API 的变化应该被记录下来 +- 检查 Travis 测试结果,检查代码/文档格式和单元测试。 + +合并 PR 的方式有两种: + +- 在 Github 上,按照[说明](https://gitbox.apache.org/setup/)将你的 Apache 账户与 + Github 账户链接,之后你就可以直接在 GitHub 上合并 PR 了。 +- 通过命令行合并 pull request + 到https://github.com/apache/singa/pull/xxx,应执行以下指令: + + ```shell + git clone https://github.com/apache/singa.git + git remote add asf https://gitbox.apache.org/repos/asf/singa.git + git fetch origin pull/xxx/head:prxxx + git checkout dev + git merge --no-ff prxxx + git push asf dev:dev + ``` + + 不要使用 rebase 来合并 PR,并禁用 fast forward。 diff --git a/docs-site/website/versioned_docs/version-4.3.0_Chinese/graph.md b/docs-site/website/versioned_docs/version-4.3.0_Chinese/graph.md new file mode 100644 index 0000000..be37486 --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0_Chinese/graph.md @@ -0,0 +1,504 @@ +--- +id: version-4.2.0_Chinese-graph +title: Model +original_id: graph +--- + + + +神经网络中的前向和反向传播可以用一组操作来表示,比如卷积和池化。每个操作都需要一 +些输入的[tensors](./tensor),并应用一个[operator](./autograd)来生成输出的张量。 +通过将每个运算符表示为一个节点,将每个张量表示为一条边,所有的运算就形成了一个计 +算图。有了计算图,可以通过调度运算的执行和内存的智能分配/释放来进行速度和内存优 +化。在 SINGA 中,用户只需要使 +用[Model](https://github.com/apache/singa/blob/master/python/singa/model.py) API +定义神经网络模型,计算图则会在 C++后台自动构建和优化。 + +这样,一方面,用户使用[Model](./graph) API 按照 PyTorch 那样的命令式编程风格实现 +网络。而与 PyTorch 在每次迭代中重新创建操作不同的是,SINGA 在第一次迭代后就会缓 +冲操作,隐式地创建计算图(当该功能被启用时)。因此,另一方面,SINGA 的计算图与使 +用声明式编程的库(如 TensorFlow)创建的计算图类似,因而它可以享受在图上进行的优 +化。 + +## 样例 + +下面的代码说明了`Model`API 的用法: + +1. 将新模型实现为 Model 类的子类: + +```Python +class CNN(model.Model): + + def __init__(self, num_classes=10, num_channels=1): + super(CNN, self).__init__() + self.conv1 = layer.Conv2d(num_channels, 20, 5, padding=0, activation="RELU") + self.conv2 = layer.Conv2d(20, 50, 5, padding=0, activation="RELU") + self.linear1 = layer.Linear(500) + self.linear2 = layer.Linear(num_classes) + self.pooling1 = layer.MaxPool2d(2, 2, padding=0) + self.pooling2 = layer.MaxPool2d(2, 2, padding=0) + self.relu = layer.ReLU() + self.flatten = layer.Flatten() + self.softmax_cross_entropy = layer.SoftMaxCrossEntropy() + + def forward(self, x): + y = self.conv1(x) + y = self.pooling1(y) + y = self.conv2(y) + y = self.pooling2(y) + y = self.flatten(y) + y = self.linear1(y) + y = self.relu(y) + y = self.linear2(y) + return y + + def train_one_batch(self, x, y): + out = self.forward(x) + loss = self.softmax_cross_entropy(out, y) + self.optimizer(loss) + return out, loss +``` + +2. 创建 model、optimizer、device 等的实例。编译模型: + +```python +model = CNN() + +# initialize optimizer and attach it to the model +sgd = opt.SGD(lr=0.005, momentum=0.9, weight_decay=1e-5) +model.set_optimizer(sgd) + +# initialize device +dev = device.create_cuda_gpu() + +# input and target placeholders for the model +tx = tensor.Tensor((batch_size, 1, IMG_SIZE, IMG_SIZE), dev, tensor.float32) +ty = tensor.Tensor((batch_size, num_classes), dev, tensor.int32) + +# compile the model before training +model.compile([tx], is_train=True, use_graph=True, sequential=False) +``` + +3. 迭代训练: + +```python +for b in range(num_train_batch): + # generate the next mini-batch + x, y = ... + + # Copy the data into input tensors + tx.copy_from_numpy(x) + ty.copy_from_numpy(y) + + # Training with one batch + out, loss = model(tx, ty) +``` + +这个例子的 Google Colab notebook 可以 +在[这里](https://colab.research.google.com/drive/1fbGUs1AsoX6bU5F745RwQpohP4bHTktq)找 +到。 + +更多例子: + +- [MLP](https://github.com/apache/singa/blob/master/examples/mlp/model.py) +- [CNN](https://github.com/apache/singa/blob/master/examples/cnn/model/cnn.py) +- [ResNet](https://github.com/apache/singa/blob/master/examples/cnn/model/resnet.py) + +## 实现 + +### 图的构建 + +SINGA 分三步构建计算图: + +1. 将操作保存在缓冲区。 +2. 分析操作的依赖性。 +3. 根据依赖关系创建节点和边。 + +以 MLP 模型的 dense 层的矩阵乘法运算为例,该操作会 +在[MLP model](https://github.com/apache/singa/blob/master/examples/mlp/model.py)的 +前向函数中被调用: + +```python +class MLP(model.Model): + + def __init__(self, data_size=10, perceptron_size=100, num_classes=10): + super(MLP, self).__init__() + self.linear1 = layer.Linear(perceptron_size) + ... + + def forward(self, inputs): + y = self.linear1(inputs) + ... +``` + +`线性`层由`mutmul`运算符组成,`autograd`通过 SWIG 调用 CPP 中提供的`Mult`函数来 +实现`matmul`运算符。 + +```python +# implementation of matmul() +singa.Mult(inputs, w) +``` + +At the backend, the `Mult` function is implemented by calling `GEMV` a CBLAS +function. Instead of calling `GEMV` directly, `Mult` submits `GEMV` and the +arguments to the device as follows, 在后端,`Mult`函数是通过调用`GEMV`一个 CBLAS +函数来实现的。但`Mult`没有直接调用`GEMV`,而是将`GEMV`和参数提交给设备,具体如下 +。 + +```c++ +// implementation of Mult() +C->device()->Exec( + [a, A, b, B, CRef](Context *ctx) mutable { + GEMV(a, A, B, b, &CRef, ctx); + }, + read_blocks, {C->block()}); +``` + +`Device`的`Exec`函数对函数及其参数进行缓冲。此外,它还拥有这个函数要读写的块的信 +息(块是指张量的内存块)。 + +一旦`Model.forward()`被执行一次,所有的操作就会被`Device`缓冲。接下来,对所有操 +作的读写信息进行分析,用来建立计算图。例如,如果一个块`b`被一个操作 O1 写入,之 +后又被另一个操作 O2 读出,我们就会知道 O2 依赖于 O1 并且有一条从 A 到 B 的有向边 +,它代表了块`b`(或其张量)。之后我们就构建了一个有向无环图,如下图所示。该图会 +构建一次。 + +![The computational graph of MLP](assets/GraphOfMLP.png) + +
**Figure 1 - MLP 例子的计算图** + +### 优化 + +目前,基于计算图进行了以下优化: + +**惰性分配** 当创建张量/块时,设备不会立即为它们分配内存。相反,是在第一次访问块 +时,才会分配内存。 + +**自动回收** 每个张量/块的参考计数是根据图计算出来的。在执行操作之前,参考计数是 +读取这个块的操作次数。在执行过程中,一旦执行了一个操作,每一个输入块的参考数就会 +减少 1,如果一个块的参考数达到了 0,就意味着这个块在剩下的操作中不会再被读取。因 +此,它的内存可以被安全释放。此外,SINGA 还会跟踪图外的块的使用情况。如果一个块被 +Python 代码使用(而不是被 autograd 操作符使用),它将不会被回收。 + +**内存共享** SINGA 使用内存池,如[CnMem](https://github.com/NVIDIA/cnmem)来管理 +CUDA 内存。有了自动回收和内存池,SINGA 就可以在张量之间共享内存。考虑两个操 +作`c=a+b`和`d=2xc`。在执行第二个操作之前,根据惰性分配原则,应该分配 d 的内存。 +假设`a`在其余操作中没有使用。根据自动回收,`a`的块将在第一次操作后被释放。因此 +,SINGA 会向 CUDA 流提交四个操作:加法、释放`a`、分配`b`和乘法。这样,内存池就可 +以将`a`释放的内存与`b`共享,而不是要求 GPU 为`b`做真正的 malloc。 + +其他的优化技术,如来自编译器的优化技术,如常见的子表达式消除和不同 CUDA 流上的并 +行化操作也可以应用。 + +## 新的操作符 + +`autograd`模块中定义的每个运算符都实现了两个功能:前向和反向,通过在后台调用运算 +符来实现。如果要在`autograd`中添加一个新的运算符,需要在后台添加多个运算符。 + +以[Conv2d](https://github.com/apache/singa/blob/master/python/singa/autograd.py)运 +算符为例,在 Python 端,根据设备类型,从后台调用运算符来实现前向和反向功能: + +```python +class _Conv2d(Operation): + + def forward(self, x, W, b=None): + ...... + if training: + if self.handle.bias_term: + self.inputs = (x, W, b) # record x, W, b + else: + self.inputs = (x, W) + + if (type(self.handle) != singa.ConvHandle): + return singa.GpuConvForward(x, W, b, self.handle) + else: + return singa.CpuConvForward(x, W, b, self.handle) + + def backward(self, dy): + if (type(self.handle) != singa.ConvHandle): + dx = singa.GpuConvBackwardx(dy, self.inputs[1], self.inputs[0], + self.handle) + dW = singa.GpuConvBackwardW(dy, self.inputs[0], self.inputs[1], + self.handle) + db = singa.GpuConvBackwardb( + dy, self.inputs[2], + self.handle) if self.handle.bias_term else None + else: + dx = singa.CpuConvBackwardx(dy, self.inputs[1], self.inputs[0], + self.handle) + dW = singa.CpuConvBackwardW(dy, self.inputs[0], self.inputs[1], + self.handle) + db = singa.CpuConvBackwardb( + dy, self.inputs[2], + self.handle) if self.handle.bias_term else None + if db: + return dx, dW, db + else: + return dx, dW +``` + +对于后台的每一个操作符,应按以下方式实现: + +- 假设操作符是`foo()`,它的真正实现应该包装在另一个函数中,例 + 如`_foo()`。`foo()`将`_foo`和参数一起作为 lambda 函数传递给`Device`的`Exec`函 + 数进行缓冲,要读和写的块也同时被传递给`Exec`。 + +- lambda 表达式中使用的所有参数都需要根据以下规则获取: + + - `值捕获`: 如果参数变量是一个局部变量,或者将被立刻释放(例如,中间时序)。否 + 则,一旦`foo()`存在,这些变量将被销毁。 + - `引用捕获`:如果变量是记录在 python 端或者是一个持久变量(例如 Conv2d 类中的 + 参数 W 和 ConvHand)。 + + - `可变捕获`: 如果在`_foo()`中修改了由值捕获的变量,则 lambda 表达式应带有 + mutable(可变)标签。 + +下面是一个在后台实现的操作 +的[例子](https://github.com/apache/singa/blob/master/src/model/operation/convolution.cc): + +```c++ +Tensor GpuConvBackwardx(const Tensor &dy, const Tensor &W, const Tensor &x, + const CudnnConvHandle &cch) { + CHECK_EQ(dy.device()->lang(), kCuda); + + Tensor dx; + dx.ResetLike(x); + + dy.device()->Exec( + /* + * dx is a local variable so it's captured by value + * dy is an intermediate tensor and isn't recorded on the python side + * W is an intermediate tensor but it's recorded on the python side + * chh is a variable and it's recorded on the python side + */ + [dx, dy, &W, &cch](Context *ctx) mutable { + Block *wblock = W.block(), *dyblock = dy.block(), *dxblock = dx.block(); + float alpha = 1.f, beta = 0.f; + cudnnConvolutionBackwardData( + ctx->cudnn_handle, &alpha, cch.filter_desc, wblock->data(), + cch.y_desc, dyblock->data(), cch.conv_desc, cch.bp_data_alg, + cch.workspace.block()->mutable_data(), + cch.workspace_count * sizeof(float), &beta, cch.x_desc, + dxblock->mutable_data()); + }, + {dy.block(), W.block()}, {dx.block(), cch.workspace.block()}); + /* the lambda expression reads the blocks of tensor dy and w + * and writes the blocks of tensor dx and chh.workspace + */ + + return dx; +} +``` + +## Benchmark + +### 单节点 + +- 实验设定 + - 模型: + - 使用层: ResNet50 in + [resnet.py](https://github.com/apache/singa/blob/master/examples/cnn/autograd/resnet_cifar10.py) + - 使用模型: ResNet50 in + [resnet.py](https://github.com/apache/singa/blob/master/examples/cnn/model/resnet.py) + - GPU: NVIDIA RTX 2080Ti +- 注释: + - `s` :second,秒 + - `it` : iteration,迭代次数 + - `Mem`:peak memory usage of single GPU,单 GPU 显存峰值 + - `Throughout`:number of images processed per second,每秒处理的图像数 + - `Time`:total time,总时间 + - `Speed`:iterations per second。每秒迭代次数 + - `Reduction`:the memory usage reduction rate compared with that using + layer,与使用层的内存使用率相比,内存使用率降低了多少 + - `Speedup`: speedup ratio compared with dev branch,与 dev 分支相比的加速率 +- 结果: + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
BatchsizeCasesMem(MB)Time(s)Speed(it/s)ThroughputReductionSpeedup
16layer497514.195214.0893225.42850.00%1.0000
model:disable graph499514.126414.1579226.5261-0.40%1.0049
model:enable graph, bfs328313.743814.5520232.831834.01%1.0328
model:enable graph, serial326513.742014.5540232.863534.37%1.0330
32layer1011913.45877.4302237.76490.00%1.0000
model:disable graph1010913.29527.5315240.68750.10%1.0123
model:enable graph, bfs683913.10597.6302244.164832.41%1.0269
model:enable graph, serial684513.04897.6635245.231232.35%1.0314
+ +### 多线程 + +- 实验设置: + - API: + - 使用层: ResNet50 in + [resnet_dist.py](https://github.com/apache/singa/blob/master/examples/cnn/autograd/resnet_dist.py) + - 使用模型: ResNet50 in + [resnet.py](https://github.com/apache/singa/blob/master/examples/cnn/model/resnet.py) + - GPU: NVIDIA RTX 2080Ti \* 2 + - MPI: 在同一节点上的两个 MPI processes +- 注释: 与上面相同 +- 结果: + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
BatchsizeCasesMem(MB)Time(s)Speed(it/s)ThroughputReductionSpeedup
16layer543917.332311.5391369.25220.00%1.0000
model:disable graph542717.823211.2213359.08310.22%0.9725
model:enable graph, bfs338918.231010.9703351.050437.69%0.9507
model:enable graph, serial343717.038911.7378375.610336.81%1.0172
32layer1054714.86356.7279430.58580.00%1.0000
model:disable graph1050314.77466.7684433.17480.42%1.0060
model:enable graph, bfs693514.85536.7316430.823134.25%1.0006
model:enable graph, serial702714.32716.9798446.707433.37%1.0374
+ +### 结论 + +- 在启用计算图的情况下进行训练,可以显著减少内存占用。 +- 目前,在速度上有一点改进。在效率方面还可以做更多的优化。 diff --git a/docs-site/website/versioned_docs/version-4.3.0_Chinese/half-precision.md b/docs-site/website/versioned_docs/version-4.3.0_Chinese/half-precision.md new file mode 100644 index 0000000..c4a454f --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0_Chinese/half-precision.md @@ -0,0 +1,115 @@ +--- +id: version-4.2.0_Chinese-half-precision +title: Half Precision +original_id: half-precision +--- + + + +Half precision training 优点: + +- CPU 内存使用低, 网络支持大。 +- 训练速度快。 + +## Half data type + +### Half data type 定义 + +在 IEEE 754 标准中明确 binary16 有如下格式: +[format](https://en.wikipedia.org/wiki/Half-precision_floating-point_format): +Sign bit: 1 bit Exponent width: 5 bits Significand precision: 11 bits (10 +explicitly stored) + +### Half data type 运算 + +以 fp32 形式加载数据,快速转换成 fp16。 + +```python +>>> from singa import tensor, device +>>> dev = device.create_cuda_gpu() +>>> x = tensor.random((2,3),dev) +>>> x +[[0.7703407 0.42764223 0.5872884 ] + [0.78362167 0.70469785 0.64975065]], float32 +>>> y = x.as_type(tensor.float16) +>>> y +[[0.7705 0.4277 0.5874] + [0.7837 0.7046 0.65 ]], float16 +``` + +初级运算支持 fp16 格式。 + +```python +>>> y+y +[[1.541 0.8555 1.175 ] + [1.567 1.409 1.3 ]], float16 +``` + +## Training in Half + +### Training in Half 三个步骤 + +半精度训练只需要如下三个步骤: + +1. 加载数据并且转换成半精度数据 +2. 设置数据优化类型 +3. 启动训练模型 + +```python +# cast input data to fp16 +x = load_data() +x = x.astype(np.float16) +tx = tensor.from_numpy(x) + +# load model +model = build_model() +# set optimizer dtype to fp16 +sgd = opt.SGD(lr=0.1, dtype=tensor.float16) + +# train as usual +out, loss = model(tx, ty) +``` + +### 示例 + +提供示例脚本`train_cnn.py`,可执行下面的命令语句开始半精度模型训练。 + +```python +python examples/cnn/train_cnn.py cnn mnist -pfloat16 +``` + +## 实现 + +### Half Type 依赖性 + +该半精度实现方式就像一半半精度模型支持的一样,是被整合在 C++后端来实现的。 + +在 GPU 上跑的时候,`__half`可用在 uda math API 中,为了支持`__half`数学运算,需 +要编译 Nvidia compute arch > 6.0(Pascal) + +### Nvidia Hardware Acceleration: Tensor Core + +Nvidia 发布 Tensor Core 后进一步加速了半精度和倍数吞吐量的运算,如 GEMM(CuBlas) +and convolution(CuDNN)。要启用 Tensor core 的运算,在 GEMM 方面有一些要求,比如 +:卷积通道大小,Cuda 版本和 GPU 版本(图灵或更高版本)等等。 + +### Implement Operations + +半精度运算起初被整合在`tensor_math_cuda.h`中,专门提供半精度类型运算模版和实现方 +式,用来实现低数据量的计算。 + +示例, GEMM 运算实现如下: + +```c++ +template <> +void GEMM(const half_float::half alpha, + const Tensor& A, const Tensor& B, + const half_float::half beta, Tensor* C, + Context* ctx) { + // ... + CUBLAS_CHECK(cublasGemmEx(handle, transb, transa, ncolB, nrowA, ncolA, + alphaPtr, BPtr, Btype, ldb, APtr, Atype, lda, + betaPtr, CPtr, Ctype, ldc, computeType, algo)); + // ... +} +``` diff --git a/docs-site/website/versioned_docs/version-4.3.0_Chinese/history-singa.md b/docs-site/website/versioned_docs/version-4.3.0_Chinese/history-singa.md new file mode 100644 index 0000000..90b1b34 --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0_Chinese/history-singa.md @@ -0,0 +1,40 @@ +--- +id: version-4.2.0_Chinese-history-singa +title: History of SINGA +original_id: history-singa +--- + + + +## 历史 + +SINGA 由新加坡国立大学 DB System Group 于 2014 年发起,与浙江大学数据库组合作。 +如果您在研究中使用 SINGA,请引用以下两篇论文。 + +- B.C. Ooi, K.-L. Tan, S. Wang, W. Wang, Q. Cai, G. Chen, J. Gao, Z. Luo, A. K. + H. Tung, Y. Wang, Z. Xie, M. Zhang, and K. Zheng. + [SINGA: A distributed deep learning platform](http://www.comp.nus.edu.sg/~ooibc/singaopen-mm15.pdf). + ACM Multimedia (Open Source Software Competition) 2015 + +- W. Wang, G. Chen, T. T. A. Dinh, B. C. Ooi, K.-L.Tan, J. Gao, and S. Wang. + [SINGA: putting deep learning in the hands of multimedia users](http://www.comp.nus.edu.sg/~ooibc/singa-mm15.pdf). + ACM Multimedia 2015. + +Rafiki 是 SINGA 的一个子模块,如果您在研究中使用 Rafiki,请引用以下论文。 + +- Wei Wang, Jinyang Gao, Meihui Zhang, Sheng Wang, Gang Chen, Teck Khim Ng, Beng + Chin Ooi, Jie Shao, Moaz Reyad. + [Rafiki: Machine Learning as an Analytics Service System](http://www.vldb.org/pvldb/vol12/p128-wang.pdf). + [VLDB 2019](http://vldb.org/2019/) + ([BibTex](https://dblp.org/rec/bib2/journals/pvldb/WangWGZCNOS18.bib)). + +[NetEase](http://tech.163.com/17/0602/17/CLUL016I00098GJ5.html), +[yzBigData](http://www.yzbigdata.com/en/index.html), +[Shentilium](https://shentilium.com/), [Foodlg](http://www.foodlg.com/) 以及 +[Medilot](https://medilot.com/technologies) 等公司在他们的工作和应用中使用了 +SINGA。 + +## License + +SINGA 以[Apache License Version 2.0](http://www.apache.org/licenses/LICENSE-2.0) +版本发布。 diff --git a/docs-site/website/versioned_docs/version-4.3.0_Chinese/how-to-release.md b/docs-site/website/versioned_docs/version-4.3.0_Chinese/how-to-release.md new file mode 100644 index 0000000..47bfcfa --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0_Chinese/how-to-release.md @@ -0,0 +1,195 @@ +--- +id: version-4.2.0_Chinese-how-to-release +title: How to Prepare a Release +original_id: how-to-release +--- + + + +这是 SINGA 的[发布准备流程](http://www.apache.org/dev/release-publishing.html)指 +南。 + +1. 选择一个发布管理者。发布管理者(RM)是发布过程的协调者,他的签名文件(.asc) + 将会与发布一起上传。RM 生成 KEY (RSA 4096 位)并将其上传到公钥服务器,首先需要 + 得到其他 Apache 用户对他的密钥的认可(签名),才能连接到信任网,RM 需要先求助 + 其他项目管理者帮忙认证他的密钥 + 。[如何生成密钥?](http://www.apache.org/dev/release-signing.html) + +2. 检查 license。 [FAQ](https://www.apache.org/legal/src-headers.html#faq-docs); + [SINGA Issue](https://issues.apache.org/jira/projects/SINGA/issues/SINGA-447) + + - 代码库不能包含与 APL 不兼容的第三方代码。 + - 依赖项与 APL 兼容,GNU 类 license 不兼容。 + - 我们编写的所有源文件都必须包含 Apache license 头 + :http://www.apache.org/legal/src-headers.html. 链接中有一个脚本可以帮助将 + 这个头同步到所有文件。 + - 更新 LICENSE 文件。如果我们在发行包中包含了任何非 APL 的第三方代码,必须要 + 在 NOTICE 文件的最后注明。 + +3. 复查版本。检查代码和文档。 + + - 编译过程无错误。 + - (尽可能地)包含进单元测试。 + - Conda 包运行无误。 + - Apache 网站上的在线文档是最新的。 + +4. 准备好 RELEASE_NOTES 文件。包括以下项目,介绍,特性,错误(链接到 JIRA 或 + Github PR),变更,依赖列表,不兼容问题,可以按照这 + 个[例子](<(http://commons.apache.org/proper/commons-digester/commons-digester-3.0/RELEASE-NOTES.txt)>)来 + 写。 + +5. 打包候选版本。该版本应该打包成:apache-singa-VERSION.tar.gz。这个版本不应该包 + 含任何二进制文件,包括 git 文件。但是 CMake 的编译依赖于 git 标签来获取版本号 + ;要删除这个依赖,你需要手动更新 CMakeLists.txt 文件来设置版本号: + + ``` + # remove the following lines + include(GetGitRevisionDescription) + git_describe(VERSION --tags --dirty=-d) + string(REGEX REPLACE "^([0-9]+)\\..*" "\\1" VERSION_MAJOR "${VERSION}") + string(REGEX REPLACE "^[0-9]+\\.([0-9]+).*" "\\1" VERSION_MINOR "${VERSION}") + string(REGEX REPLACE "^[0-9]+\\.[0-9]+\\.([0-9]+).*" "\\1" VERSION_PATCH "${VERSION}") + + # set the numbers manually + SET(PACKAGE_VERSION 3.0.0) + SET(VERSION 3.0.0) + SET(SINGA_MAJOR_VERSION 3) # 0 - + SET(SINGA_MINOR_VERSION 0) # 0 - 9 + SET(SINGA_PATCH_VERSION 0) # 0 - 99 + ``` + + 将软件包上传到[stage repo](https://dist.apache.org/repos/dist/dev/singa/)。应 + 包括 tar 文件、签名、KEY 和 SHA256 校验和文件。MD5 不再使用,详细规则 + 在[这里](https://dist.apache.org/repos/dist/dev/singa/)。阶段文件夹应该包括: + + - apache-singa-VERSION.tar.gz + - apache-singa-VERSION.acs + - apache-singa-VERSION.SHA256 + + 创建这些文件并上传到 stage svn repo 的命令如下: + + ```sh + # in singa repo + rm -rf .git + rm -rf rafiki/* + cd .. + tar -czvf apache-singa-VERSION.tar.gz singa/ + + mkdir stage + cd stage + svn co https://dist.apache.org/repos/dist/dev/singa/ + cd singa + # copy the KEYS file from singa repo to this folder if it is not here + cp ../../singa/KEYS . + mkdir VERSION + # copy the tar.gz file + mv ../../apache-singa-VERSION.tar.gz VERSION/ + cd VERSION + sha512sum apache-singa-VERSION.tar.gz > apache-singa-VERSION.tar.gz.sha512 + gpg --armor --output apache-singa-VERSION.tar.gz.asc --detach-sig apache-singa-VERSION.tar.gz + cd .. + svn add VERSION + svn commit + ``` + +6) 通过发送电子邮件的方式进行投票。现举例如下: + +``` + To: dev@singa.apache.org + Subject: [VOTE] Release apache-singa-X.Y.Z (release candidate N) + + Hi all, + + I have created a build for Apache SINGA 3.1.0, release candidate 2. + + The release note is at + https://github.com/apache/singa/blob/master/RELEASE_NOTES. + + The artifacts to be voted on are located here: + https://dist.apache.org/repos/dist/dev/singa/3.1.0.rc2/apache-singa-3.1.0.rc2.tar.gz +   + The hashes of the artifacts are as follows: + SHA512: 84545499ad36da108c6a599edd1d853f82d331bc03273b5278515554866f0c698e881f956b2eabcb6b29c07fa9fa4ff1add5a777b58db8a6a2362cf383b5c04d  + + Release artifacts are signed with the followingkey: + https://dist.apache.org/repos/dist/dev/singa/KEYS + + The signature file is: + https://dist.apache.org/repos/dist/dev/singa/3.1.0.rc2/apache-singa-3.1.0.rc2.tar.gz.asc + + The Github tag is at: + https://github.com/apache/singa/releases/tag/3.1.0.rc2 + + The documentation website is at + http://singa.apache.org/docs/next/installation/ + + Some examples are available for testing: + https://github.com/apache/singa/tree/master/examples + + + + Please vote on releasing this package. The vote is open for at least 72 hours and passes if a majority of at least three +1 votes are cast. + + [ ] +1 Release this package as Apache SINGA X.Y.Z + + [ ] 0 I don't feel strongly about it, but I'm okay with the release + + [ ] -1 Do not release this package because... + + Here is my vote: +1 + +``` + +7. 等待至少 48 小时的测试回复。任何 PMC、提交者或贡献者都可以测试发布的功能,以 + 及反馈。大家在投票+1 之前应该检查这些。如果投票通过,则发送如下的结果邮件,否 + 则,从头开始重复刚刚的步骤。 + +``` + +To: dev@singa.apache.org Subject: [RESULT][vote] Release apache-singa-X.Y.Z +(release candidate N) + +Thanks to everyone who has voted and given their comments. The tally is as +follows. + +N binding +1s: + +N non-binding +1s: + +No 0s or -1s. + +I am delighted to announce that the proposal to release Apache SINGA X.Y.Z has +passed. + +``` + +8. 将软件包上传至 + https://dist.apache.org/repos/dist/release/singa/,以便[distribution](http://www.apache.org/dev/release-publishing.html#distribution)。 + +9. 更新 SINGA 网站的下载页面。tar.gz 文件必须从镜像下载,使用 closer.cgi 脚本; + 其他工件必须从 Apache 主站点下载。更多细节请 + 看[这里](http://www.apache.org/dev/release-download-pages.html)。我们在之前的 + 版本中得到的一些反馈。“下载页面必须只链接到正式发布的版本,所以不能包含到 + GitHub 的链接”,“链接到 KEYS, sig 和 Hash 的链接不能使用 dist.apache.org 而应 + 该使用 + https://www.apache.org/dist/singa/...”“而且你只需要一个KEYS链接,而且应该描述如何使用KEYS+sig或Hash来验证下载。” + +10. 删除 RC 标签并编译 conda 包。 + +11. 发布 release 信息: + +``` +To: announce@apache.org, dev@singa.apache.org +Subject: [ANNOUNCE] Apache SINGA X.Y.Z released + +We are pleased to announce that SINGA X.Y.Z is released. + +SINGA is a general distributed deep learning platform +for training big deep learning models over large datasets. +The release is available at: http://singa.apache.org/downloads.html +The main features of this release include XXX +We look forward to hearing your feedback, suggestions, +and contributions to the project. + +On behalf of the SINGA team, {SINGA Team Member Name} +``` diff --git a/docs-site/website/versioned_docs/version-4.3.0_Chinese/install-win.md b/docs-site/website/versioned_docs/version-4.3.0_Chinese/install-win.md new file mode 100644 index 0000000..e299c26 --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0_Chinese/install-win.md @@ -0,0 +1,391 @@ +--- +id: version-4.2.0_Chinese-install-win +title: Build SINGA on Windows +original_id: install-win +--- + + + +在 Microsoft Windows 上从源码构建 SINGA 的过程有四个部分:安装依赖关系、构建 +SINGA 源码、(可选择)安装 python 模块和(可选择)运行单元测试。 + +## 安装依赖项 + +你可以创建一个文件夹来构建依赖关系。 + +使用到的依赖项有: + +- 编译器和 IDE: + - Visual Studio,社区版是免费的,可以用来构建 SINGA。 + https://www.visualstudio.com/ +- CMake + - 可以从 http://cmake.org/ 下载 + - 确保 cmake 可执行文件的路径在系统路径中,或者在调用 cmake 时使用完整路径。 +- SWIG + + - 可以从 http://swig.org/ 下载 + - 确保 swig 可执行文件的路径在系统路径中,或者在调用 swig 时使用完整路径。请使 + 用最新的版本,如 3.0.12。 + +- Protocol Buffers + + - 下载一个合适的版本,如 2.6.1: + https://github.com/google/protobuf/releases/tag/v2.6.1 。 + - 下载 protobuf-2.6.1.zip 和 protoc-2.6.1-win32.zip。 + - 将这两个文件解压到 dependencies 文件夹中,将 protoc 可执行文件的路径添加到系 + 统路径中,或者在调用它时使用完整路径。 + - 打开 Visual Studio solution,它可以在 vsproject 文件夹中找到。 + - 将 build settings 改为 Release 和 x64。 + - 构建 libprotobuf 项目。 + +- Openblas + + - 从 http://www.openblas.net 下载合适的源码,如 0.2.20。 + - 将源码解压到 dependencies 文件夹中。 + - 如果你没有安装 Perl,请下载一个 perl 环境,如 Strawberry Perl + (http://strawberryperl.com/)。 + - 在源文件夹中运行此命令来构建 Visual Studio 解决方案: + + ```bash + cmake -G "Visual Studio 15 2017 Win64" + ``` + + - 打开 Visual Studio 解决方案并将 build settings 更改为 Release 和 x64。 + - 构建 libopenblas 项目。 + +- Google glog + - 下载一个合适的版本,如 0.3.5: https://github.com/google/glog/releases + - 将源码解压到 dependencies 文件夹中。 + - 打开 Visual Studio solution. + - 将 build settings 改为 Release and x64. + - 构建 libglog 项目。 + +## 构建 SINGA 源代码 + +- 下载 SINGA 源代码 +- 编译 protobuf 文件: + + - 在 src/proto 目录中: + + ```shell + mkdir python_out + protoc.exe *.proto --python_out python_out + ``` + +- 为 C++和 Python 生成 swig 接口:在 src/api 目录中: + + ```shell + swig -python -c++ singa.i + ``` + +- 生成 SINGA 的 Visual Studio 解决方案:在 SINGA 源码根目录中: + + ```shell + mkdir build + cd build + ``` + +- 调用 cmake 并添加系统路径,类似于下面的例子: + + ```shell + cmake -G "Visual Studio 15 2017 Win64" ^ + -DGLOG_INCLUDE_DIR="D:/WinSinga/dependencies/glog-0.3.5/src/windows" ^ + -DGLOG_LIBRARIES="D:/WinSinga/dependencies/glog-0.3.5/x64/Release" ^ + -DCBLAS_INCLUDE_DIR="D:/WinSinga/dependencies/openblas-0.2.20/lapack-netlib/CBLAS/include" ^ + -DCBLAS_LIBRARIES="D:/WinSinga/dependencies/openblas-0.2.20/lib/RELEASE" ^ + -DProtobuf_INCLUDE_DIR="D:/WinSinga/dependencies/protobuf-2.6.1/src" ^ + -DProtobuf_LIBRARIES="D:/WinSinga/dependencies/protobuf-2.6.1/vsprojects/x64/Release" ^ + -DProtobuf_PROTOC_EXECUTABLE="D:/WinSinga/dependencies/protoc-2.6.1-win32/protoc.exe" ^ + .. + ``` + +- 在 Visual Studio 中打开生成的解决方案。 +- 将构建设置改为 Release 和 x64。 +- 将 src/api 中的 singa_wrap.cxx 文件添加到 singa_objects 项目中。 +- 在 singa_objects 项目中,打开 Additional Include Directories。 +- 添加 Python 的 include 路径。 +- 添加 numpy 的 include 路径。 +- 添加 protobuf 的 include 路径。 +- 在 singa_objects 项目的预处理程序定义中, 添加 USE_GLOG。 +- 构建 singa_objects 项目。 + +- 在 singa 项目中: + + - 将 singa_wrap.obj 添加到对象库。 + - 将目标名称改为"\_singa_wrap"。 + - 将目标扩展名为.pyd。 + - 将配置类型改为动态库(.dll)。 + - 进入 Additional Library Directories,添加路径到 python、openblas、protobuf + 和 glog 库。 + - 在 Additional Dependencies 中添加 libopenblas.lib、libglog.lib 和 + libprotobuf.lib。 + +- 构建 singa 项目 + +## 安装 python 模块 + +- 将 build/python/setup.py 中的`_singa_wrap.so`改为`_singa_wrap.pyd`。 +- 将`src/proto/python_out`中的文件复制到`build/python/singa/proto`中。 + +- (可选)创建并激活一个虚拟环境: + + ```shell + mkdir SingaEnv + virtualenv SingaEnv + SingaEnv\Scripts\activate + ``` + +- 进入 build/python 文件夹,运行: + + ```shell + python setup.py install + ``` + +- 将 \_singa_wrap.pyd、libglog.dll 和 libopenblas.dll 添加到路径中,或者将它们复 + 制到 python site-packages 中的 singa package 文件夹中。 + +* 通过运行如下命令,来验证 SINGA 是否安装成功: + + ```shell + python -c "from singa import tensor" + ``` + +构建过程的视频教程可以在这里找到: + +[![youtube video](https://img.youtube.com/vi/cteER7WeiGk/0.jpg)](https://www.youtube.com/watch?v=cteER7WeiGk) + +## 运行单元测试 + +- 在测试文件夹中,生成 Visual Studio 解决方案: + + ```shell + cmake -G "Visual Studio 15 2017 Win64" + ``` + +- 在 Visual Studio 中打开生成的解决方案。 + +- 更改 build settings 为 Release 和 x64。 + +- 构建 glog 项目。 + +- 在 test_singa 项目中: + + - 将 USE_GLOG 添加到 Preprocessor Definitions 中。 + - 在 Additional Include Directories 中, 添加上面第 2 步中使用的 + GLOG_INCLUDE_DIR、 CBLAS_INCLUDE_DIR 和 Protobuf_INCLUDE_DIR 的路径。同时添 + 加 build 和 build/include 文件夹。 + - 转到 Additional Library Directories,添加 openblas、protobuf 和 glog 库的路 + 径。同时添加 build/src/singa_objects.dir/Release。 + - 转到 Additional Dependencies 并添加 libopenblas.lib、libglog.lib 和 + libprotobuf.lib。修改两个库的名字:gtest.lib 和 singa_objects.lib。 + +- 构建 test_singa 项目。 + +- 将 libglog.dll 和 libopenblas.dll 添加到路径中,或者将它们复制到 test/release + 文件夹中,使其可用。 + +- 单元测试可以通过如下方式执行: + + - 从命令行: + + ```shell + test_singa.exe + ``` + + - 从 Visual Studio: + - 右键点击 test_singa 项目,选择 "Set as StartUp Project"。 + - 在 Debug 菜单中,选择'Start Without Debugging'。 + +单元测试的视频教程可以在这里找到: + +[![youtube video](https://img.youtube.com/vi/393gPtzMN1k/0.jpg)](https://www.youtube.com/watch?v=393gPtzMN1k) + +## 构建包含 cuda 的 GPU 支持 + +在本节中,我们将扩展前面的步骤来启用 GPU。 + +### 安装依赖项 + +除了上面第 1 节的依赖关系外,我们还需要以下内容: + +- CUDA + + 从 https://developer.nvidia.com/cuda-downloads 下载一个合适的版本,如 9.1。确 + 保已经安装了 Visual Studio 集成模块。 + +* cuDNN + + 从 https://developer.nvidia.com/cudnn 下载一个合适的版本,如 7.1。 + +* cnmem: + + - 从 https://github.com/NVIDIA/cnmem 下载最新版本。 + - 构建 Visual Studio 解决方案: + + ```shell + cmake -G "Visual Studio 15 2017 Win64" + ``` + + - 在 Visual Studio 中打开生成的解决方案。 + - 将 build settings 改为 Release 和 x64。 + - 构建 cnmem 项目。 + +### 构建 SINGA 源代码 + +- 调用 cmake 并添加系统路径,类似于下面的例子: + ```shell + cmake -G "Visual Studio 15 2017 Win64" ^ + -DGLOG_INCLUDE_DIR="D:/WinSinga/dependencies/glog-0.3.5/src/windows" ^ + -DGLOG_LIBRARIES="D:/WinSinga/dependencies/glog-0.3.5/x64/Release" ^ + -DCBLAS_INCLUDE_DIR="D:/WinSinga/dependencies/openblas-0.2.20/lapack-netlib/CBLAS/include" ^ + -DCBLAS_LIBRARIES="D:/WinSinga/dependencies/openblas-0.2.20/lib/RELEASE" ^ + -DProtobuf_INCLUDE_DIR="D:/WinSinga/dependencies/protobuf-2.6.1/src" ^ + -DProtobuf_LIBRARIES="D:\WinSinga/dependencies/protobuf-2.6.1/vsprojects/x64/Release" ^ + -DProtobuf_PROTOC_EXECUTABLE="D:/WinSinga/dependencies/protoc-2.6.1-win32/protoc.exe" ^ + -DCUDNN_INCLUDE_DIR=D:\WinSinga\dependencies\cudnn-9.1-windows10-x64-v7.1\cuda\include ^ + -DCUDNN_LIBRARIES=D:\WinSinga\dependencies\cudnn-9.1-windows10-x64-v7.1\cuda\lib\x64 ^ + -DSWIG_DIR=D:\WinSinga\dependencies\swigwin-3.0.12 ^ + -DSWIG_EXECUTABLE=D:\WinSinga\dependencies\swigwin-3.0.12\swig.exe ^ + -DUSE_CUDA=YES ^ + -DCUDNN_VERSION=7 ^ + .. + ``` + +* 为 C++和 Python 生成 swig 接口。在 src/api 目录中: + + ```shell + swig -python -c++ singa.i + ``` + +* 在 Visual Studio 中打开生成的解决方案 + +* 将 build settings 改为 Release 和 x64 + +#### 构建 singa_objects + +- 将 src/api 中的 singa_wrap.cxx 文件添加到 singa_objects 项目中。 +- 在 singa_objects 项目中,打开 Additional Include Directories。 +- 添加 Python 的 include 路径 +- 添加 numpy include 路径 +- 添加 protobuf 包括路径 +- 增加 CUDA、cuDNN 和 cnmem 的包含路径。 +- 在 singa_objects 项目的预处理程序定义中, 加入 USE_GLOG、 USE_CUDA 和 + USE_CUDNN。删除 DISABLE_WARNINGS。 +- 建立 singa_objects 项目 + +#### 构建 singa-kernel + +- 创建一个新的 Visual Studio 项目,类型为 "CUDA 9.1 Runtime"。给它起个名字,比如 + singa-kernel。 +- 该项目自带一个名为 kernel.cu 的初始文件,从项目中删除这个文件。 +- 添加这个文件:src/core/tensor/math_kernel.cu。 +- 在项目设置中。 + + - 将平台工具集设置为 "Visual Studio 2015 (v140)" + - 将 "配置类型 "设置为 "静态库(.lib)" + - 在 include 目录中,添加 build/include。 + +- 建立 singa-kernel 项目 + +#### 构建 singa + +- 在 singa 项目中: + + - 将 singa_wrap.obj 添加到对象库中。 + - 将目标名称改为"\_singa_wrap"。 + - 将目标扩展名为.pyd。 + - 将配置类型改为动态库(.dll)。 + - 到 Additional Library Directories 中添加 python、openblas 的路径。protobuf + 和 glog 库。 + - 同时添加 singa-kernel、cnmem、cuda 和 cudnn 的 library path。 + - 到 Additional Dependencies,并添加 libopenblas.lib、libglog.lib 和 + libprotobuf.lib。 + - 另外还要添加 + :singa-kernel.lib、cnmem.lib、cudnn.lib、cuda.lib、cublas.lib。curand.lib + 和 cudart.lib。 + +- 构建 singa 项目。 + +### Install Python module + +- 将 build/python/setup.py 中的 \_singa_wrap.so 改为 \_singa_wrap.pyd。 + +- 将 src/proto/python_out 中的文件复制到 build/python/singa/proto 中。 + +- (可选) 创建并激活虚拟环境: + + ```shell + mkdir SingaEnv + virtualenv SingaEnv + SingaEnv\Scripts\activate + ``` + +- 进入 build/python 文件夹,运行: + + ```shell + python setup.py install + ``` + +- 将 \_singa_wrap.pyd, libglog.dll, libopenblas.dll, cnmem.dll, CUDA Runtime (例 + 如 cudart64_91.dll) 和 cuDNN (例如 cudnn64_7.dll) 添加到路径中,或者将它们复制 + 到 python site-packages 中的 singa package 文件夹中。 + +- 通过运行如下命令来验证 SINGA 是否已经安装: + + ```shell + python -c "from singa import device; dev = device.create_cuda_gpu()" + ``` + +这个部分的视频教程可以在这里找到: + +[![youtube video](https://img.youtube.com/vi/YasKVjRtuDs/0.jpg)](https://www.youtube.com/watch?v=YasKVjRtuDs) + +### 运行单元测试 + +- 在测试文件夹中,生成 Visual Studio 解决方案: + + ```shell + cmake -G "Visual Studio 15 2017 Win64" + ``` + +- 在 Visual Studio 中打开生成的解决方案,或者将项目添加到步骤 5.2 中创建的 singa + 解决方案中。 + +- 将 build settings 改为 Release 和 x64。 + +- 构建 glog 项目。 + +- 在 test_singa 项目中: + + - 将 USE_GLOG; USE_CUDA; USE_CUDNN 添加到 Preprocessor Definitions 中。 + - 在 Additional Include Directories 中, 添加上面 5.2 中使用的 + GLOG_INCLUDE_DIR、 CBLAS_INCLUDE_DIR 和 Protobuf_INCLUDE_DIR 的路径。同时添 + 加 build、build/include、CUDA 和 cuDNN 的 include 文件夹。 + - 转到 Additional Library Directories,添加 openblas、protobuf 和 glog 库的路 + 径。同时添加 build/src/singa_objects.dir/Release、singa-kernel、cnmem、CUDA + 和 cuDNN 库的路径。 + - 在 Additional Dependencies 中添加 libopenblas.lib; libglog.lib; + libprotobuf.lib; cnmem.lib; cudnn.lib; cuda.lib; cublas.lib; curand.lib; + cudart.lib; singa-kernel.lib。修正两个库的名字:gtest.lib 和 + singa_objects.lib。 + +* 构建. + +* 将 libglog.dll、libopenblas.dll、cnmem.dll、cudart64_91.dll 和 cudnn64_7.dll + 添加到路径中,或将它们复制到 test/release 文件夹中,使其可用。 + +* 单元测试可以通过如下方式执行: + + - 从命令行: + + ```shell + test_singa.exe + ``` + + - 从 Visual Studio: + - 右键点击 test_singa 项目,选择 'Set as StartUp Project'. + - 从 Debug 菜单,选择 'Start Without Debugging' + +运行单元测试的视频教程可以在这里找到: + +[![youtube video](https://img.youtube.com/vi/YOjwtrvTPn4/0.jpg)](https://www.youtube.com/watch?v=YOjwtrvTPn4) diff --git a/docs-site/website/versioned_docs/version-4.3.0_Chinese/installation.md b/docs-site/website/versioned_docs/version-4.3.0_Chinese/installation.md new file mode 100644 index 0000000..cb1637b --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0_Chinese/installation.md @@ -0,0 +1,132 @@ +--- +id: version-4.3.0_Chinese-installation +title: Installation +original_id: installation +--- + + + +## 使用 pip + +1. 只使用 CPU + [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/17RA056Brwk0vBQTFaZ-l9EbqwADO0NA9?usp=sharing) + +```bash +pip install singa -f http://singa.apache.org/docs/next/wheel-cpu.html --trusted-host singa.apache.org +``` + +您可以通过`singa==`安装特定版本的 SINGA,其中``字段应被替换, +例如`2.1.0`。可用的 SINGA 版本在链接中列出。 + +要安装最新的开发版本,请将链接替换为 +http://singa.apache.org/docs/next/wheel-cpu-dev.html + +2. 使用 CUDA 和 cuDNN 的 GPU + [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1W30IPCqj5fG8ADAQsFqclaCLyIclVcJL?usp=sharing) + +```bash +pip install singa -f http://singa.apache.org/docs/next/wheel-gpu.html --trusted-host singa.apache.org +``` + +您也可以配置 SINGA 版本和 CUDA 版本,`s4.3.0+cuda10.2`,SINGA 版本和 CUDA 版 +本的可用组合在链接中列出。 + +要安装最新的开发版本,请将链接替换为 +http://singa.apache.org/docs/next/wheel-gpu-dev.html + +注意:你本地 Python 环境的 Python 版本将被用来寻找相应的 wheel 包。例如,如果你 +本地的 Python 是 3.9,那么就会通过 pip 选择在 Python 3.9 上编译的 wheel 包并安装 +。事实上,wheel 文件的名称包括 SINGA 版本、CUDA 版本和 Python 版本。因此 +,`pip`知道要下载和安装哪个 wheel 文件。 + +参考 setup.py 文件顶部的注释,了解如何构建 wheel 包。 + +如果运行以下命令没有报错: + +```shell +$ python -c "from singa import tensor" +``` + +那么 SINGA 就安装成功了。 + +## 使用 Docker + +按照[说明](https://docs.docker.com/install/)在你的本地主机上安装 Docker。将您的 +用户添加 +到[docker 组](https://docs.docker.com/install/linux/linux-postinstall/)中,以便 +在没有`sudo`的情况下运行 docker 命令。 + +1. 仅使用 CPU + +```shell +$ docker run -it apache/singa:X.Y.Z-cpu-ubuntu16.04 /bin/bash +``` + +2. 要使用 GPU,在安装 Docker 后安装 + [Nvidia-Docker](https://github.com/NVIDIA/nvidia-docker) + +```shell +$ nvidia-docker run -it apache/singa:X.Y.Z-cuda9.0-cudnn7.4.2-ubuntu16.04 /bin/bash +``` + +3. 关于 SINGA Docker 镜像(标签)的完整列表,请访 + 问[docker hub site](https://hub.docker.com/r/apache/singa/)。对于每个 docker + 镜像,标签的命名为: + +```shell +version-(cpu|gpu)[-devel] +``` + +| Tag | Description | Example value | +| --------- | -------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `version` | SINGA version | '2.0.0-rc0', '2.0.0', '1.2.0' | +| `cpu` | the image cannot run on GPUs | 'cpu' | +| `gpu` | the image can run on Nvidia GPUs | 'gpu', or 'cudax.x-cudnnx.x' e.g., 'cuda10.0-cudnn7.3' | +| `devel` | indicator for development | if absent, SINGA Python package is installed for runtime only; if present, the building environment is also created, you can recompile SINGA from source at '/root/singa' | +| `OS` | indicate OS version number | 'ubuntu16.04', 'ubuntu18.04' | + +## 从源码编译 + +您可以使用本地构建工具或 conda-build 在本地主机操作系统上或在 Docker 容器中从源 +代码[构建和安装 SINGA](build.md)。 + +## FAQ + +- Q: `from singa import tensor`错误 + + A: 执行下面的命令,检查详细的错误: + + ```shell + python -c "from singa import _singa_wrap" + # go to the folder of _singa_wrap.so + ldd path to _singa_wrap.so + python + >> import importlib + >> importlib.import_module('_singa_wrap') + ``` + + `_singa_wrap.so` 的文件夹是 + `~/miniconda3/lib/python3.10/site-packages/singa`。通常情况下,这个错误是由于 + 依赖的库不匹配或缺失造成的,例如 cuDNN 或 protobuf。解决方法是创建一个新的虚拟 + 环境,并在该环境中安装 SINGA,例如: + +```shell +conda create -n singa +conda activate singa +conda install -c nusdbsystem -c conda-forge singa-cpu +``` + +- Q: 使用虚拟环境时,每次安装 SINGA 时,都会重新安装 numpy。但是,当我运 + 行`import numpy`时,numpy 没有被使用。 + + A: 这可能是由`PYTHONPATH`环境变量引起的,在使用虚拟环境时,应将其设置为空,以 + 避免与虚拟环境的路径冲突。 + +- Q: 当我在 Mac OS X 中运行 SINGA 时,得到如下错误 "Fatal Python error: + PyThreadState_Get: no current thread Abort trap: 6" + + A: 这个错误通常发生在系统中有多个 Python 版本的时候,例如,操作系统自带的版本 + 和 Homebrew 安装的版本。SINGA 链接的 Python 必须与 Python 解释器相同。您可以通 + 过`which python`来检查解释器 python 版本并通 + 过`otool -L ` 检查 SINGA 链接的 Python,如果通过 conda + 安装 SINGA,这个问题应该可以解决。 diff --git a/docs-site/website/versioned_docs/version-4.3.0_Chinese/issue-tracking.md b/docs-site/website/versioned_docs/version-4.3.0_Chinese/issue-tracking.md new file mode 100644 index 0000000..fda489d --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0_Chinese/issue-tracking.md @@ -0,0 +1,12 @@ +--- +id: version-4.2.0_Chinese-issue-tracking +title: Issue Tracking +original_id: issue-tracking +--- + + + +SINGA 使用[JIRA](https://issues.apache.org/jira/browse/singa)来管理问题,包括 +bug、新功能和讨论。 + +我们现在正在转移到[Github Issues](https://github.com/apache/singa/issues)。 diff --git a/docs-site/website/versioned_docs/version-4.3.0_Chinese/mail-lists.md b/docs-site/website/versioned_docs/version-4.3.0_Chinese/mail-lists.md new file mode 100644 index 0000000..4681073 --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0_Chinese/mail-lists.md @@ -0,0 +1,15 @@ +--- +id: version-4.2.0_Chinese-mail-lists +title: Project Mailing Lists +original_id: mail-lists +--- + + + +这些是为这个项目建立的邮件列表。每个列表都有一个订阅、退订和归档链接: + +| Name | Post | Subscribe | Unsubscribe | Archive | +| ----------- | ------------------------------------ | ---------------------------------------------------------------- | -------------------------------------------------------------------- | ----------------------------------------------------------------------------------- | +| Development | | [Subscribe](mailto:dev-subscribe@singa.incubator.apache.org) | [Unsubscribe](mailto:dev-unsubscribe@singa.incubator.apache.org.) | [mail-archives.apache.org](http://mail-archives.apache.org/mod_mbox/singa-dev/) | +| Commits | | [Subscribe](mailto:commits-subscribe@singa.incubator.apache.org) | [Unsubscribe](mailto:commits-unsubscribe@singa.incubator.apache.org) | [mail-archives.apache.org](http://mail-archives.apache.org/mod_mbox/singa-commits/) | +| Security | | private | private | private | diff --git a/docs-site/website/versioned_docs/version-4.3.0_Chinese/onnx.md b/docs-site/website/versioned_docs/version-4.3.0_Chinese/onnx.md new file mode 100644 index 0000000..7bd8ba6 --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0_Chinese/onnx.md @@ -0,0 +1,737 @@ +--- +id: version-4.2.0_Chinese-onnx +title: ONNX +original_id: onnx +--- + + + +[ONNX](https://onnx.ai/) 是机器学习模型的开放表示格式,它使 AI 开发人员能够在不 +同的库和工具中使用模型。SINGA 支持加载 ONNX 格式模型用于训练和 inference,并将使 +用 SINGA API(如[Module](./module))定义的模型保存为 ONNX 格式。 + +SINGA 在以 +下[版本](https://github.com/onnx/onnx/blob/master/docs/Versioning.md)中的 ONNX +中测试过。 + +| ONNX version | File format version | Opset version ai.onnx | Opset version ai.onnx.ml | Opset version ai.onnx.training | +| ------------ | ------------------- | --------------------- | ------------------------ | ------------------------------ | +| 1.6.0 | 6 | 11 | 2 | - | + +## 通常用法 + +### 从 ONNX 中读取一个 Model 到 SINGA + +在通过 `onnx.load` 从磁盘加载 ONNX 模型后,您需要更新模型的 batch_size,因为对于 +大多数模型来说,它们使用一个占位符来表示其批处理量。我们在这里举一个例子,若要 +`update_batch_size`,你只需要更新输入和输出的 batch_size,内部的 tensors 的形状 +会自动推断出来。 + +然后,您可以使用 `sonnx.prepare` 来准备 SINGA 模型。该函数将 ONNX 模型图中的所有 +节点迭代并翻译成 SINGA 运算符,加载所有存储的权重并推断每个中间张量的形状。 + +```python3 +import onnx +from singa import device +from singa import sonnx + +# if the input has multiple tensors? can put this function inside prepare()? +def update_batch_size(onnx_model, batch_size): + model_input = onnx_model.graph.input[0] + model_input.type.tensor_type.shape.dim[0].dim_value = batch_size + model_output = onnx_model.graph.output[0] + model_output.type.tensor_type.shape.dim[0].dim_value = batch_size + return onnx_model + + +model_path = "PATH/To/ONNX/MODEL" +onnx_model = onnx.load(model_path) + +# set batch size +onnx_model = update_batch_size(onnx_model, 1) + +# convert onnx graph nodes into SINGA operators +dev = device.create_cuda_gpu() +sg_ir = sonnx.prepare(onnx_model, device=dev) +``` + +### Inference SINGA 模型 + +一旦创建了模型,就可以通过调用`sg_ir.run`进行 inference。输入和输出必须是 SINGA +Tensor 实例,由于 SINGA 模型以列表形式返回输出,如果只有一个输出,你只需要从输出 +中取第一个元素即可。 + +```python3 +# can warp the following code in prepare() +# and provide a flag training=True/False? + +class Infer: + + + def __init__(self, sg_ir): + self.sg_ir = sg_ir + + def forward(self, x): + return sg_ir.run([x])[0] + + +data = get_dataset() +x = tensor.Tensor(device=dev, data=data) + +model = Infer(sg_ir) +y = model.forward(x) +``` + +### 将 SINGA 模型保存成 ONNX 格式 + +给定输入时序和输出时序,由运算符产生的模型,你可以追溯所有内部操作。因此,一个 +SINGA 模型是由输入和输出张量定义的,要将 SINGA 模型导出为 ONNX 格式,您只需提供 +输入和输出张量列表。 + +```python3 +# x is the input tensor, y is the output tensor +sonnx.to_onnx([x], [y]) +``` + +### 在 ONNX 模型上重新训练 + +要使用 SINGA 训练(或改进)ONNX 模型,您需要设置内部的张量为可训练状态: + +```python3 +class Infer: + + def __init__(self, sg_ir): + self.sg_ir = sg_ir + ## can wrap these codes in sonnx? + for idx, tens in sg_ir.tensor_map.items(): + # allow the tensors to be updated + tens.requires_grad = True + tens.stores_grad = True + + def forward(self, x): + return sg_ir.run([x])[0] + +autograd.training = False +model = Infer(sg_ir) + +autograd.training = True +# then you training the model like normal +# give more details?? +``` + +### 在 ONNX 模型上做迁移学习 + +您也可以在 ONNX 模型的最后附加一些图层来进行转移学习。`last_layers` 意味着您从 +[0, last_layers] 切断 ONNX 层。然后您可以通过普通的 SINGA 模型附加更多的层。 + +```python3 +class Trans: + + def __init__(self, sg_ir, last_layers): + self.sg_ir = sg_ir + self.last_layers = last_layers + self.append_linear1 = autograd.Linear(500, 128, bias=False) + self.append_linear2 = autograd.Linear(128, 32, bias=False) + self.append_linear3 = autograd.Linear(32, 10, bias=False) + + def forward(self, x): + y = sg_ir.run([x], last_layers=self.last_layers)[0] + y = self.append_linear1(y) + y = autograd.relu(y) + y = self.append_linear2(y) + y = autograd.relu(y) + y = self.append_linear3(y) + y = autograd.relu(y) + return y + +autograd.training = False +model = Trans(sg_ir, -1) + +# then you training the model like normal +``` + +## 一个完整示例 + +本部分以 mnist 为例,介绍 SINGA ONNX 的使用方法。在这部分,将展示如何导出、加载 +、inference、再训练和迁移学习 mnist 模型的例子。您可以 +在[这里](https://colab.research.google.com/drive/1-YOfQqqw3HNhS8WpB8xjDQYutRdUdmCq)试 +用这部分内容。 + +### 读取数据集 + +首先,你需要导入一些必要的库,并定义一些辅助函数来下载和预处理数据集: + +```python +import os +import urllib.request +import gzip +import numpy as np +import codecs + +from singa import device +from singa import tensor +from singa import opt +from singa import autograd +from singa import sonnx +import onnx + + +def load_dataset(): + train_x_url = 'http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz' + train_y_url = 'http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz' + valid_x_url = 'http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz' + valid_y_url = 'http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz' + train_x = read_image_file(check_exist_or_download(train_x_url)).astype( + np.float32) + train_y = read_label_file(check_exist_or_download(train_y_url)).astype( + np.float32) + valid_x = read_image_file(check_exist_or_download(valid_x_url)).astype( + np.float32) + valid_y = read_label_file(check_exist_or_download(valid_y_url)).astype( + np.float32) + return train_x, train_y, valid_x, valid_y + + +def check_exist_or_download(url): + + download_dir = '/tmp/' + + name = url.rsplit('/', 1)[-1] + filename = os.path.join(download_dir, name) + if not os.path.isfile(filename): + print("Downloading %s" % url) + urllib.request.urlretrieve(url, filename) + return filename + + +def read_label_file(path): + with gzip.open(path, 'rb') as f: + data = f.read() + assert get_int(data[:4]) == 2049 + length = get_int(data[4:8]) + parsed = np.frombuffer(data, dtype=np.uint8, offset=8).reshape( + (length)) + return parsed + + +def get_int(b): + return int(codecs.encode(b, 'hex'), 16) + + +def read_image_file(path): + with gzip.open(path, 'rb') as f: + data = f.read() + assert get_int(data[:4]) == 2051 + length = get_int(data[4:8]) + num_rows = get_int(data[8:12]) + num_cols = get_int(data[12:16]) + parsed = np.frombuffer(data, dtype=np.uint8, offset=16).reshape( + (length, 1, num_rows, num_cols)) + return parsed + + +def to_categorical(y, num_classes): + y = np.array(y, dtype="int") + n = y.shape[0] + categorical = np.zeros((n, num_classes)) + categorical[np.arange(n), y] = 1 + categorical = categorical.astype(np.float32) + return categorical +``` + +### MNIST 模型 + +然后你可以定义一个叫做**CNN**的类来构造 mnist 模型,这个模型由几个卷积层、池化层 +、全连接层和 relu 层组成。你也可以定义一个函数来计算我们结果的**准确性**。最后, +你可以定义一个**训练函数**和一个**测试函数**来处理训练和预测的过程。 + +```python +class CNN: + def __init__(self): + self.conv1 = autograd.Conv2d(1, 20, 5, padding=0) + self.conv2 = autograd.Conv2d(20, 50, 5, padding=0) + self.linear1 = autograd.Linear(4 * 4 * 50, 500, bias=False) + self.linear2 = autograd.Linear(500, 10, bias=False) + self.pooling1 = autograd.MaxPool2d(2, 2, padding=0) + self.pooling2 = autograd.MaxPool2d(2, 2, padding=0) + + def forward(self, x): + y = self.conv1(x) + y = autograd.relu(y) + y = self.pooling1(y) + y = self.conv2(y) + y = autograd.relu(y) + y = self.pooling2(y) + y = autograd.flatten(y) + y = self.linear1(y) + y = autograd.relu(y) + y = self.linear2(y) + return y + + +def accuracy(pred, target): + y = np.argmax(pred, axis=1) + t = np.argmax(target, axis=1) + a = y == t + return np.array(a, "int").sum() / float(len(t)) + + +def train(model, + x, + y, + epochs=1, + batch_size=64, + dev=device.get_default_device()): + batch_number = x.shape[0] // batch_size + + for i in range(epochs): + for b in range(batch_number): + l_idx = b * batch_size + r_idx = (b + 1) * batch_size + + x_batch = tensor.Tensor(device=dev, data=x[l_idx:r_idx]) + target_batch = tensor.Tensor(device=dev, data=y[l_idx:r_idx]) + + output_batch = model.forward(x_batch) + # onnx_model = sonnx.to_onnx([x_batch], [y]) + # print('The model is:\n{}'.format(onnx_model)) + + loss = autograd.softmax_cross_entropy(output_batch, target_batch) + accuracy_rate = accuracy(tensor.to_numpy(output_batch), + tensor.to_numpy(target_batch)) + + sgd = opt.SGD(lr=0.001) + for p, gp in autograd.backward(loss): + sgd.update(p, gp) + sgd.step() + + if b % 1e2 == 0: + print("acc %6.2f loss, %6.2f" % + (accuracy_rate, tensor.to_numpy(loss)[0])) + print("training completed") + return x_batch, output_batch + +def test(model, x, y, batch_size=64, dev=device.get_default_device()): + batch_number = x.shape[0] // batch_size + + result = 0 + for b in range(batch_number): + l_idx = b * batch_size + r_idx = (b + 1) * batch_size + + x_batch = tensor.Tensor(device=dev, data=x[l_idx:r_idx]) + target_batch = tensor.Tensor(device=dev, data=y[l_idx:r_idx]) + + output_batch = model.forward(x_batch) + result += accuracy(tensor.to_numpy(output_batch), + tensor.to_numpy(target_batch)) + + print("testing acc %6.2f" % (result / batch_number)) +``` + +### 训练 mnist 模型并将其导出到 onnx + +现在,你可以通过调用 **soonx.to_onnx** 函数来训练 mnist 模型并导出其 onnx 模型。 + +```python +def make_onnx(x, y): + return sonnx.to_onnx([x], [y]) + +# create device +dev = device.create_cuda_gpu() +#dev = device.get_default_device() +# create model +model = CNN() +# load data +train_x, train_y, valid_x, valid_y = load_dataset() +# normalization +train_x = train_x / 255 +valid_x = valid_x / 255 +train_y = to_categorical(train_y, 10) +valid_y = to_categorical(valid_y, 10) +# do training +autograd.training = True +x, y = train(model, train_x, train_y, dev=dev) +onnx_model = make_onnx(x, y) +# print('The model is:\n{}'.format(onnx_model)) + +# Save the ONNX model +model_path = os.path.join('/', 'tmp', 'mnist.onnx') +onnx.save(onnx_model, model_path) +print('The model is saved.') +``` + +### Inference + +导出 onnx 模型后,可以在'/tmp'目录下找到一个名为**mnist.onnx**的文件,这个模型可 +以被其他库导入。现在,如果你想把这个 onnx 模型再次导入到 singa 中,并使用验证数 +据集进行推理,你可以定义一个叫做**Infer**的类,Infer 的前向函数将被测试函数调用 +,对验证数据集进行推理。此外,你应该把训练的标签设置为**False**,以固定自变量算 +子的梯度。 + +在导入 onnx 模型时,需要先调用**onnx.load**来加载 onnx 模型。然后将 onnx 模型输 +入到 **soonx.prepare**中进行解析,并启动到一个 singa 模型(代码中 +的**sg_ir**)。sg_ir 里面包含了一个 singa 图,然后就可以通过输入到它的 run 函数中 +运行一步推理。 + +```python +class Infer: + def __init__(self, sg_ir): + self.sg_ir = sg_ir + for idx, tens in sg_ir.tensor_map.items(): + # allow the tensors to be updated + tens.requires_grad = True + tens.stores_grad= True + sg_ir.tensor_map[idx] = tens + + def forward(self, x): + return sg_ir.run([x])[0] # we can run one step of inference by feeding input + +# load the ONNX model +onnx_model = onnx.load(model_path) +sg_ir = sonnx.prepare(onnx_model, device=dev) # parse and initiate to a singa model + +# inference +autograd.training = False +print('The inference result is:') +test(Infer(sg_ir), valid_x, valid_y, dev=dev) +``` + +### 重训练 + +假设导入模型后,想再次对模型进行重新训练,我们可以定义一个名为**re_train**的函数 +。在调用这个 re_train 函数之前,我们应该将训练的标签设置为**True**,以使自变量运 +算符更新其梯度。而在完成训练后,我们再将其设置为**False**,以调用做推理的测试函 +数。 + +```python +def re_train(sg_ir, + x, + y, + epochs=1, + batch_size=64, + dev=device.get_default_device()): + batch_number = x.shape[0] // batch_size + + new_model = Infer(sg_ir) + + for i in range(epochs): + for b in range(batch_number): + l_idx = b * batch_size + r_idx = (b + 1) * batch_size + + x_batch = tensor.Tensor(device=dev, data=x[l_idx:r_idx]) + target_batch = tensor.Tensor(device=dev, data=y[l_idx:r_idx]) + + output_batch = new_model.forward(x_batch) + + loss = autograd.softmax_cross_entropy(output_batch, target_batch) + accuracy_rate = accuracy(tensor.to_numpy(output_batch), + tensor.to_numpy(target_batch)) + + sgd = opt.SGD(lr=0.01) + for p, gp in autograd.backward(loss): + sgd.update(p, gp) + sgd.step() + + if b % 1e2 == 0: + print("acc %6.2f loss, %6.2f" % + (accuracy_rate, tensor.to_numpy(loss)[0])) + print("re-training completed") + return new_model + +# load the ONNX model +onnx_model = onnx.load(model_path) +sg_ir = sonnx.prepare(onnx_model, device=dev) + +# re-training +autograd.training = True +new_model = re_train(sg_ir, train_x, train_y, dev=dev) +autograd.training = False +test(new_model, valid_x, valid_y, dev=dev) +``` + +### 迁移学习 + +最后,如果我们想做迁移学习,我们可以定义一个名为**Trans**的函数,在 onnx 模型后 +追加一些层。为了演示,代码中只在 onnx 模型后追加了几个线性(全连接)和 relu。可 +以定义一个 transfer_learning 函数来处理 transfer-learning 模型的训练过程,而训练 +的标签和前面一个的一样。 + +```python +class Trans: + def __init__(self, sg_ir, last_layers): + self.sg_ir = sg_ir + self.last_layers = last_layers + self.append_linear1 = autograd.Linear(500, 128, bias=False) + self.append_linear2 = autograd.Linear(128, 32, bias=False) + self.append_linear3 = autograd.Linear(32, 10, bias=False) + + def forward(self, x): + y = sg_ir.run([x], last_layers=self.last_layers)[0] + y = self.append_linear1(y) + y = autograd.relu(y) + y = self.append_linear2(y) + y = autograd.relu(y) + y = self.append_linear3(y) + y = autograd.relu(y) + return y + +def transfer_learning(sg_ir, + x, + y, + epochs=1, + batch_size=64, + dev=device.get_default_device()): + batch_number = x.shape[0] // batch_size + + trans_model = Trans(sg_ir, -1) + + for i in range(epochs): + for b in range(batch_number): + l_idx = b * batch_size + r_idx = (b + 1) * batch_size + + x_batch = tensor.Tensor(device=dev, data=x[l_idx:r_idx]) + target_batch = tensor.Tensor(device=dev, data=y[l_idx:r_idx]) + output_batch = trans_model.forward(x_batch) + + loss = autograd.softmax_cross_entropy(output_batch, target_batch) + accuracy_rate = accuracy(tensor.to_numpy(output_batch), + tensor.to_numpy(target_batch)) + + sgd = opt.SGD(lr=0.07) + for p, gp in autograd.backward(loss): + sgd.update(p, gp) + sgd.step() + + if b % 1e2 == 0: + print("acc %6.2f loss, %6.2f" % + (accuracy_rate, tensor.to_numpy(loss)[0])) + print("transfer-learning completed") + return trans_mode + +# load the ONNX model +onnx_model = onnx.load(model_path) +sg_ir = sonnx.prepare(onnx_model, device=dev) + +# transfer-learning +autograd.training = True +new_model = transfer_learning(sg_ir, train_x, train_y, dev=dev) +autograd.training = False +test(new_model, valid_x, valid_y, dev=dev) +``` + +## ONNX 模型库 + +[ONNX 模型库](https://github.com/onnx/models)是由社区成员贡献的 ONNX 格式的预先 +训练的最先进模型的集合。SINGA 现在已经支持了几个 CV 和 NLP 模型。将来会支持更多 +模型。 + +### 图像分类 + +这套模型以图像作为输入,然后将图像中的主要物体分为 1000 个物体类别,如键盘、鼠标 +、铅笔和许多动物。 + +| Model Class | Reference | Description | Link | +| --------------------------------------------------------------------------------------------------- | ------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| [MobileNet](https://github.com/onnx/models/tree/master/vision/classification/mobilenet) | [Sandler et al.](https://arxiv.org/abs/1801.04381) | 最适合移动和嵌入式视觉应用的轻量级深度神经网络。
Top-5 error from paper - ~10% | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1HsixqJMIpKyEPhkbB8jy7NwNEFEAUWAf) | +| [ResNet18](https://github.com/onnx/models/tree/master/vision/classification/resnet) | [He et al.](https://arxiv.org/abs/1512.03385) | 一个 CNN 模型(多达 152 层),在对图像进行分类时,使用 shortcut 来实现更高的准确性。
Top-5 error from paper - ~3.6% | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1u1RYefSsVbiP4I-5wiBKHjsT9L0FxLm9) | +| [VGG16](https://github.com/onnx/models/tree/master/vision/classification/vgg) | [Simonyan et al.](https://arxiv.org/abs/1409.1556) | 深度 CNN 模型(多达 19 层)。类似于 AlexNet,但使用多个较小的内核大小的滤波器,在分类图像时提供更高的准确性。
Top-5 error from paper - ~8% | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/14kxgRKtbjPCKKsDJVNi3AvTev81Gp_Ds) | +| [ShuffleNet_V2](https://github.com/onnx/models/tree/master/vision/classification/shufflenet) | [Simonyan et al.](https://arxiv.org/pdf/1707.01083.pdf) | 专门为移动设备设计的计算效率极高的 CNN 模型。这种网络架构设计考虑了速度等直接指标,而不是 FLOP 等间接指标。 Top-1 error from paper - ~30.6% | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/19HfRu3YHP_H2z3BcZujVFRp23_J5XsuA?usp=sharing) | + +### 目标检测 + +目标检测模型可以检测图像中是否存在多个对象,并将图像中检测到对象的区域分割出来。 + +| Model Class | Reference | Description | Link | +| ----------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------- | ------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------- | +| [Tiny YOLOv2](https://github.com/onnx/models/tree/master/vision/object_detection_segmentation/tiny_yolov2) | [Redmon et al.](https://arxiv.org/pdf/1612.08242.pdf) | 一个用于目标检测的实时 CNN,可以检测 20 个不同的类。一个更复杂的完整 YOLOv2 网络的小版本。 | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/11V4I6cRjIJNUv5ZGsEGwqHuoQEie6b1T) | + +### 面部识别 + +人脸检测模型可以识别和/或识别给定图像中的人脸和情绪。 + +| Model Class | Reference | Description | Link | +| --------------------------------------------------------------------------------------------------------- | -------------------------------------------------- | ----------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- | +| [ArcFace](https://github.com/onnx/models/tree/master/vision/body_analysis/arcface) | [Deng et al.](https://arxiv.org/abs/1801.07698) | 一种基于 CNN 的人脸识别模型,它可以学习人脸的判别特征,并对输入的人脸图像进行分析。 | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1qanaqUKGIDtifdzEzJOHjEj4kYzA9uJC) | +| [Emotion FerPlus](https://github.com/onnx/models/tree/master/vision/body_analysis/emotion_ferplus) | [Barsoum et al.](https://arxiv.org/abs/1608.01041) | 基于人脸图像训练的情感识别深度 CNN。 | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1XHtBQGRhe58PDi4LGYJzYueWBeWbO23r) | + +### 机器理解 + +这个自然语言处理模型的子集,可以回答关于给定上下文段落的问题。 + +| Model Class | Reference | Description | Link | +| ----------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| [BERT-Squad](https://github.com/onnx/models/tree/master/text/machine_comprehension/bert-squad) | [Devlin et al.](https://arxiv.org/pdf/1810.04805.pdf) | 该模型根据给定输入段落的上下文回答问题。 | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1kud-lUPjS_u-TkDAzihBTw0Vqr0FjCE-) | +| [RoBERTa](https://github.com/onnx/models/tree/master/text/machine_comprehension/roberta) | [Devlin et al.](https://arxiv.org/pdf/1907.11692.pdf) | 一个基于大型变换器的模型,根据给定的输入文本预测情感。 | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1F-c4LJSx3Cb2jW6tP7f8nAZDigyLH6iN?usp=sharing) | +| [GPT-2](https://github.com/onnx/models/tree/master/text/machine_comprehension/gpt-2) | [Devlin et al.](https://d4mucfpksywv.cloudfront.net/better-language-models/language_models_are_unsupervised_multitask_learners.pdf) | 一个基于大型变换器的语言模型,给定一些文本中的单词序列,预测下一个单词。 | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1ZlXLSIMppPch6HgzKRillJiUcWn3PiK7?usp=sharing) | + +## 支持的操作符 + +onnx 支持下列运算: + +- Acos +- Acosh +- Add +- And +- Asin +- Asinh +- Atan +- Atanh +- AveragePool +- BatchNormalization +- Cast +- Ceil +- Clip +- Concat +- ConstantOfShape +- Conv +- Cos +- Cosh +- Div +- Dropout +- Elu +- Equal +- Erf +- Expand +- Flatten +- Gather +- Gemm +- GlobalAveragePool +- Greater +- HardSigmoid +- Identity +- LeakyRelu +- Less +- Log +- MatMul +- Max +- MaxPool +- Mean +- Min +- Mul +- Neg +- NonZero +- Not +- OneHot +- Or +- Pad +- Pow +- PRelu +- Reciprocal +- ReduceMean +- ReduceSum +- Relu +- Reshape +- ScatterElements +- Selu +- Shape +- Sigmoid +- Sign +- Sin +- Sinh +- Slice +- Softmax +- Softplus +- Softsign +- Split +- Sqrt +- Squeeze +- Sub +- Sum +- Tan +- Tanh +- Tile +- Transpose +- Unsqueeze +- Upsample +- Where +- Xor + +### 对 ONNX 后端的特别说明 + +- Conv, MaxPool 以及 AveragePool + + 输入必须是 1d`(N*C*H)`和 2d`(N*C*H*W)`的形状,`dilation`必须是 1。 + +- BatchNormalization + + `epsilon` 设定为 1e-05,不能改变 + +- Cast + + 只支持 float32 和 int32,其他类型都会转向这两种类型。 + +- Squeeze and Unsqueeze + + 如果你在`Tensor`和 Scalar 之间`Squeeze`或`Unsqueeze`时遇到错误,请向我们报告。 + +- Empty tensor + + 空张量在 SINGA 是非法的。 + +## 实现 + +SINGA ONNX 的代码在`python/singa/soonx.py`中,主要有三个类 +,`SingaFrontend`、`SingaBackend`和`SingaRep`。`SingaFrontend`将 SINGA 模型翻译 +成 ONNX 模型;`SingaBackend`将 ONNX 模型翻译成`SingaRep`对象,其中存储了所有的 +SINGA 运算符和张量(本文档中的张量指 SINGA Tensor);`SingaRep`可以像 SINGA 模型 +一样运行。 + +### SingaFrontend + +`SingaFrontend`的入口函数是`singa_to_onnx_model`,它也被称 +为`to_onnx`,`singa_to_onnx_model`创建了 ONNX 模型,它还通 +过`singa_to_onnx_graph`创建了一个 ONNX 图。 + +`singa_to_onnx_graph`接受模型的输出,并从输出中递归迭代 SINGA 模型的图,得到所有 +的运算符,形成一个队列。SINGA 模型的输入和中间张量,即可训练的权重,同时被获取。 +输入存储在`onnx_model.graph.input`中;输出存储在`onnx_model.graph.output`中;可 +训练权重存储在`onnx_model.graph.initializer`中。 + +然后将队列中的 SINGA 运算符逐一翻译成 ONNX 运算符。`_rename_operators` 定义了 +SINGA 和 ONNX 之间的运算符名称映射。`_special_operators` 定义了翻译运算符时要使 +用的函数。 + +此外,SINGA 中的某些运算符与 ONNX 的定义不同,即 ONNX 将 SINGA 运算符的某些属性 +视为输入,因此 `_unhandled_operators` 定义了处理特殊运算符的函数。 + +由于 SINGA 中的布尔类型被视为 int32,所以`_bool_operators`定义了要改变的操作符为 +布尔类型。 + +### SingaBackend + +`SingaBackend`的入口函数是`prepare`,它检查 ONNX 模型的版本,然后调 +用`_onnx_model_to_singa_net`。 + +`_onnx_model_to_singa_net`的目的是获取 SINGA 的时序和运算符。tensors 在 ONNX 中 +以其名称存储在字典中,而操作符则 +以`namedtuple('SingaOps', ['name', 'op', 'handle', 'forward'])`的形式存储在队列 +中。对于每个运算符,`name`是它的 ONNX 节点名称;`op`是 ONNX 节点;`forward`是 +SINGA 运算符的转发函数;`handle`是为一些特殊的运算符准备的,如 Conv 和 Pooling, +它们有`handle`对象。 + +`_onnx_model_to_singa_net`的第一步是调用`_init_graph_parameter`来获取模型内的所 +有 tensors。对于可训练的权重,可以从`onnx_model.graph.initializer`中初始 +化`SINGA Tensor`。请注意,权重也可能存储在图的输入或称为`Constant`的 ONNX 节点中 +,SINGA 也可以处理这些。 + +虽然所有的权重都存储在 ONNX 模型中,但模型的输入是未知的,只有它的形状和类型。所 +以 SINGA 支持两种方式来初始化输入,1、根据其形状和类型生成随机张量,2、允许用户 +分配输入。第一种方法对大多数模型都很好,但是对于一些模型,比如 BERT,矩阵的指数 +不能随机生成,否则会产生错误。 + +然后,`_onnx_model_to_singa_net`迭代 ONNX 图中的所有节点,将其翻译成 SIGNA 运算 +符。另外,`_rename_operators` 定义了 SINGA 和 ONNX 之间的运算符名称映射 +。`_special_operators` 定义翻译运算符时要使用的函数。`_run_node`通过输入时序来运 +行生成的 SINGA 模型,并存储其输出时序,供以后的运算符使用。 + +该类最后返回一个`SingaRep`对象,并在其中存储所有 SINGA 时序和运算符。 + +### SingaRep + +`SingaBackend`存储所有的 SINGA tensors 和运算符。`run`接受模型的输入,并按照运算 +符队列逐个运行 SINGA 运算符。用户可以使用`last_layers`来决定是否将模型运行到最后 +几层。将 `all_outputs` 设置为 `False` 表示只得到最后的输出,设置为 `True` 表示也 +得到所有的中间输出。 diff --git a/docs-site/website/versioned_docs/version-4.3.0_Chinese/optimizer.md b/docs-site/website/versioned_docs/version-4.3.0_Chinese/optimizer.md new file mode 100644 index 0000000..ac4b41a --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0_Chinese/optimizer.md @@ -0,0 +1,125 @@ +--- +id: version-4.2.0_Chinese-optimizer +title: Optimizer +original_id: optimizer +--- + + + +SINGA 支持各种流行的优化器,包括动量随机梯度下降、Adam、RMSProp 和 AdaGrad 等。 +对于每一种优化器,它都支持使用衰减调度器来安排不同时间段的学习率。优化器和衰减调 +度器包含在`singa/opt.py`中。 + +## 创建一个优化器 + +1. 带动量的 SGD + +```python +# define hyperparameter learning rate +lr = 0.001 +# define hyperparameter momentum +momentum = 0.9 +# define hyperparameter weight decay +weight_decay = 0.0001 + +from singa import opt +sgd = opt.SGD(lr=lr, momentum=momentum, weight_decay=weight_decay) +``` + +2. RMSProp + +```python +# define hyperparameter learning rate +lr = 0.001 +# define hyperparameter rho +rho = 0.9 +# define hyperparameter epsilon +epsilon = 1e-8 +# define hyperparameter weight decay +weight_decay = 0.0001 + +from singa import opt +sgd = opt.RMSProp(lr=lr, rho=rho, epsilon=epsilon, weight_decay=weight_decay) +``` + +3. AdaGrad + +```python +# define hyperparameter learning rate +lr = 0.001 +# define hyperparameter epsilon +epsilon = 1e-8 +# define hyperparameter weight decay +weight_decay = 0.0001 + +from singa import opt +sgd = opt.AdaGrad(lr=lr, epsilon=epsilon, weight_decay=weight_decay) +``` + +4. Adam + +```python +# define hyperparameter learning rate +lr = 0.001 +# define hyperparameter beta 1 +beta_1= 0.9 +# define hyperparameter beta 2 +beta_1= 0.999 +# define hyperparameter epsilon +epsilon = 1e-8 +# define hyperparameter weight decay +weight_decay = 0.0001 + +from singa import opt +sgd = opt.Adam(lr=lr, beta_1=beta_1, beta_2=beta_2, epsilon=epsilon, weight_decay=weight_decay) +``` + +## 创建一个衰减调度器 + +```python +from singa import opt + +# define initial learning rate +lr_init = 0.001 +# define the rate of decay in the decay scheduler +decay_rate = 0.95 +# define whether the learning rate schedule is a staircase shape +staircase=True +# define the decay step of the decay scheduler (in this example the lr is decreased at every 2 steps) +decay_steps = 2 + +# create the decay scheduler, the schedule of lr becomes lr_init * (decay_rate ^ (step // decay_steps) ) +lr = opt.ExponentialDecay(0.1, 2, 0.5, True) +# Use the lr to create an optimizer +sgd = opt.SGD(lr=lr, momentum=0.9, weight_decay=0.0001) +``` + +## 使用模型 API 中的优化器 + +当我们创建模型时,我们需要将优化器附加到模型上: + +```python +# create a CNN using the Model API +model = CNN() + +# initialize optimizer and attach it to the model +sgd = opt.SGD(lr=0.005, momentum=0.9, weight_decay=1e-5) +model.set_optimizer(sgd) +``` + +然后,当我们调用模型时,它会运行利用优化器的 `train_one_batch` 方法。 + +因此,一个迭代循环优化模型的例子是: + +```python +for b in range(num_train_batch): + # generate the next mini-batch + x, y = ... + + # Copy the data into input tensors + tx.copy_from_numpy(x) + ty.copy_from_numpy(y) + + # Training with one batch + out, loss = model(tx, ty) +``` diff --git a/docs-site/website/versioned_docs/version-4.3.0_Chinese/security.md b/docs-site/website/versioned_docs/version-4.3.0_Chinese/security.md new file mode 100644 index 0000000..2275d11 --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0_Chinese/security.md @@ -0,0 +1,10 @@ +--- +id: version-4.2.0_Chinese-security +title: Security +original_id: security +--- + + + +用户可以向[SINGA 安全团队邮件列表](mailto:security@singa.apache.org)报告安全漏洞 +。 diff --git a/docs-site/website/versioned_docs/version-4.3.0_Chinese/software-stack.md b/docs-site/website/versioned_docs/version-4.3.0_Chinese/software-stack.md new file mode 100644 index 0000000..97d5573 --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0_Chinese/software-stack.md @@ -0,0 +1,116 @@ +--- +id: version-4.2.0_Chinese-software-stack +title: Software Stack +original_id: software-stack +--- + + + +SINGA 的软件栈包括两个主要层次,底层后端和 Python 接口层。图 1 展示了它们与硬件 +的关系。后端组件为深度学习模型提供基本的数据结构,为调度和执行操作提供硬件抽象, +为分布式训练提供通信组件。Python 接口封装了部分 CPP 数据结构,并为神经网络训练提 +供了额外的高级类,方便实现复杂的神经网络模型。 + +接下来,我们以自下而上的方式介绍软件栈。 + +![SINGA V3 software stack](assets/singav3.1-sw.png)
**Figure 1 - SINGA V3 +软件栈结构** + +## 底层后端 + +### Device + +每个`Device`实例,即一个设备,是针对一个硬件设备创建的,例如 GPU 或 +CPU。`Device`管理数据结构的内存,并调度操作的执行,例如,在 CUDA 流或 CPU 线程上 +。根据硬件及其编程语言,SINGA 实现了以下特定的设备类。 + +- **CudaGPU** 代表 Nvidia GPU 卡,执行单元是 CUDA 流。 +- **CppCPU** 代表一个普通的 CPU,执行单位是 CPU 线程。 +- **OpenclGPU** 代表 Nvidia 和 AMD 的普通 GPU 卡,执行单位是 CommandQueue,鉴于 + OpenCL 与许多硬件设备兼容,如 FPGA 和 ARM,OpenclGPU 可以扩展到其他设备。 + +### Tensor + +`Tensor`类表示一个多维数组,它存储模型变量,例如,来自卷积层的输入图像和特征图。 +每个`Tensor`实例(即张量)都分配在一个设备上,它管理张量的内存,并针对张量调度( +计算)操作。大多数机器学习算法可以使用(密集或稀疏)张量抽象及其操作来表达。因此 +,SINGA 将能够运行广泛的模型,包括深度学习模型和其他传统的机器学习模型。 + +### Operator + +针对张量的运算符有两类,一类是线性代数运算符,如矩阵乘法,另一类是神经网络特有的 +运算符,如卷积和池化。线性代数运算符以`Tensor`函数的形式提供,并针对不同的硬件设 +备分别实现。 + +- CppMath (tensor_math_cpp.h) 为 CppCPU 使用 Cpp 实现张量运算。 +- CudaMath (tensor_math_cuda.h) 为 CudaGPU 使用 CUDA 实现张量运算。 +- OpenclMath(tensor_math_opencl.h) 使用 OpenCL 实现了 OpenclGPU 的张量运算。 + +神经网络的具体运算符也是单独实现的,例如: + +- GpuConvFoward (convolution.h) 在 Nvidia GPU 上通过 CuDNN 实现卷积的前向函数。 +- CpuConvForward (convolution.h) 在 CPU 上使用 CPP 实现卷积的前向函数。 + +通常情况下,用户会创建一个`Device`实例,并使用它来创建多个`Tensor`实例。当用户调 +用 Tensor 函数或神经网络操作时,会调用常驻设备的相应实现。换句话说,操作符的实现 +对用户是透明的。 + +Tensor 和 Device 抽象是可扩展的,以支持使用不同编程语言的各种硬件设备。一个新的 +硬件设备将通过添加一个新的 Device 子类和相应的操作符的实现来支持。 + +在速度和内存方面的优化是由`Device`的`Scheduler`和`MemPool`完成的。例如 +,`Scheduler`根据运算符的依赖性创建一个[计算图](./graph),然后它可以优化运算符的 +执行顺序,以实现并行性和内存共享。 + +### Communicator + +`Communicator`是为了支持[分布式训练](./dist-train)。它使用套接字、MPI 和 NCCL 实 +现通信协议。通常情况下,用户只需要调用`put()`和`get()`等高级 API 来发送和接收 +tensors。针对拓扑结构、消息大小等的通信优化在内部完成。 + +## Python 接口层 + +所有的后端组件都通过 SWIG 作为 Python 模块暴露出来。此外,还添加了以下类来支持复 +杂神经网络的实现: + +### Opt + +`Opt`及其子类实现了使用参数梯度更新模型参数值的方法(如 SGD)。子 +类[DistOpt](./dist-train)通过调用`Communicator`中的方法来同步分布式训练的 worker +之间的梯度。 + +### Operator + +`Operator`包装了多个使用后端 Tensor 或神经网络运算符实现的函数。例如,前向函数和 +后向函数`ReLU`组成`ReLU`运算符。 + +### Layer + +`Layer`和它的子类用参数来包装运算符。例如,卷积和线性运算符有权重和偏置参数。 这 +些参数由相应的`Layer`类维护。 + +### Autograd + +`Autograd`通过记录运算符的前向函数的执行情况,自动按照相反的顺序调用后向函数,实 +现 +了[反向模式的自动微分](https://rufflewind.com/2016-12-30/reverse-mode-automatic-differentiation)。 +所有的函数都可以通过`Scheduler`进行缓冲,建立[计算图](./graph),以提高效率和内存 +优化。 + +### Model + +[Model](./graph)提供了一个简单的接口来实现新的网络模型。你只需要继承`Model`,通 +过创建和调用层或运算符来定义模型的前向传播。当训练数据输入到`Model`中时 +,`Model`会自动进行 autograd 并通过`Opt`自动更新参数。通过`Model` API,SINGA 充 +分利用了命令式编程和声明式编程的优势。用户使用[Model](./graph) API 按照 PyTorch +那样的命令式编程风格来实现一个网络,PyTorch 在每次迭代中都会重新创建操作,而 +SINGA 则不同,它在第一次迭代后就会缓冲操作,隐式地创建一个计算图(当该功能被启用 +时)。该图类似于使用声明式编程的库所创建的图,例如 TensorFlow。因此,SINGA 可以 +在计算图上应用内存和速度优化技术。 + +### ONNX + +为了支持 ONNX,SINGA 实现了一个[sonnx](./onnx)模块,其中包括。 + +- SingaFrontend 用于将 SINGA 模型保存为 onnx 格式。 +- SingaBackend 用于将 onnx 格式的模型加载到 SINGA 中进行训练和 inference。 diff --git a/docs-site/website/versioned_docs/version-4.3.0_Chinese/source-repository.md b/docs-site/website/versioned_docs/version-4.3.0_Chinese/source-repository.md new file mode 100644 index 0000000..eec69d4 --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0_Chinese/source-repository.md @@ -0,0 +1,24 @@ +--- +id: version-4.2.0_Chinese-source-repository +title: Source Repository +original_id: source-repository +--- + + + +本项目使用[Git](http://git-scm.com/)来管理其源代码。关于 Git 的使用说明可以在 +http://git-scm.com/documentation 找到。 + +## Repository + +以下是在线资源库的链接: + +- https://gitbox.apache.org/repos/asf?p=singa.git + +Github 镜像在: + +- https://github.com/apache/singa + +代码可以从任何一个 repo 中克隆,例如: + + git clone https://github.com/apache/singa.git diff --git a/docs-site/website/versioned_docs/version-4.3.0_Chinese/team-list.md b/docs-site/website/versioned_docs/version-4.3.0_Chinese/team-list.md new file mode 100644 index 0000000..50d581a --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0_Chinese/team-list.md @@ -0,0 +1,59 @@ +--- +id: version-4.2.0_Chinese-team-list +title: The SINGA Team +original_id: team-list +--- + + + +一个成功的项目需要许多人扮演许多角色,有些成员编写代码或文档,而其他成员则作为测 +试人员,提交补丁和建议。 + +SINGA 社区的开发者主要来自新加坡国立大学、浙江大学、网易、大阪大学、yzBigData 等 +。 + +## PMC + +| Name | Email | Organization | +| ------------- | ----------------------- | --------------------------------------------- | +| Gang Chen | cg@apache.org | Zhejiang University | +| Anh Dinh | dinhtta@apache.org | Singapore University of Technology and Design | +| Ted Dunning | tdunning@apache.org | Apache Software Foundation | +| Jinyang Gao | jinyang@apache.org | DAMO Academy, Alibaba Group | +| Alan Gates | gates@apache.org | Apache Software Foundation | +| Zhaojing Luo | zhaojing@apache.org | National University of Singapore | +| Thejas Nair | thejas@apache.org | Apache Software Foundation | +| Beng Chin Ooi | ooibc@apache.org | National University of Singapore | +| Moaz Reyad | moaz@apache.org | Université Grenoble Alpes | +| Kian-Lee Tan | tankianlee@apache.org | National University of Singapore | +| Sheng Wang | wangsh@apache.org | DAMO Academy, Alibaba Group | +| Wei Wang | wangwei@apache.org | National University of Singapore | +| Zhongle Xie | zhongle@apache.org | National University of Singapore | +| Sai Ho Yeung | chrishkchris@apache.org | National University of Singapore | +| Meihui Zhang | meihuizhang@apache.org | Beijing Institute of Technology | +| Kaiping Zheng | kaiping@apache.org | National University of Singapore | + +## Committers + +| Name | Email | Organization | +| ------------ | ---------------------- | --------------------------------------------- | +| Xiangrui Cai | caixr@apache.org | Nankai University | +| Chonho Lee | chonho@apache.org | Osaka University | +| Shicong Lin | shicong@apache.org | National University of Singapore | +| Rulin Xing | rulin@apache.org | Huazhong University of Science and Technology | +| Wanqi Xue | xuewanqi@apache.org | Nanyang Technological University | +| Joddiy Zhang | joddiyzhang@apache.org | National University of Singapore | + +## Contributors + +| Name | Email | Organization | +| ------------------ | ---------------------------- | -------------------------------- | +| Haibo Chen | hzchenhaibo@corp.netease.com | NetEase | +| Shicheng Chen | chengsc@comp.nus.edu.sg | National University of Singapore | +| Xin Ji | vincent.j.xin@gmail.com | Visenze, Singapore | +| Anthony K. H. Tung | atung@comp.nus.edu.sg | National University of Singapore | +| Ji Wang | wangji@mzhtechnologies.com | Hangzhou MZH Technologies | +| Yuan Wang | wangyuan@corp.netease.com | NetEase | +| Wenfeng Wu | dcswuw@gmail.com | Freelancer, China | +| Kaiyuan Yang | yangky@comp.nus.edu.sg | National University of Singapore | +| Chang Yao | yaochang2009@gmail.com | Hangzhou MZH Technologies | diff --git a/docs-site/website/versioned_docs/version-4.3.0_Chinese/tensor.md b/docs-site/website/versioned_docs/version-4.3.0_Chinese/tensor.md new file mode 100644 index 0000000..431562f --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0_Chinese/tensor.md @@ -0,0 +1,262 @@ +--- +id: version-4.2.0_Chinese-tensor +title: Tensor +original_id: tensor +--- + + + +每个 Tensor 实例都是分配在特定设备实例上的多维数组。Tensor 实例可以存储变量,并 +在不同类型的硬件设备上提供线性代数运算,而无需用户察觉。需要注意的是,除了复制函 +数外,用户需要确保张量操作数分配在同一个设备上。 + +## Tensor 用法 + +### 创建 Tensor + +```python +>>> import numpy as np +>>> from singa import tensor +>>> tensor.from_numpy( np.asarray([[1, 0, 0], [0, 1, 0]], dtype=np.float32) ) +[[1. 0. 0.] + [0. 1. 0.]] +``` + +### 转换到 numpy + +```python +>>> a = np.asarray([[1, 0, 0], [0, 1, 0]], dtype=np.float32) +>>> tensor.from_numpy(a) +[[1. 0. 0.] + [0. 1. 0.]] +>>> tensor.to_numpy(tensor.from_numpy(a)) +array([[1., 0., 0.], + [0., 1., 0.]], dtype=float32) +``` + +### Tensor 方法 + +```python +>>> t = tensor.from_numpy(a) +>>> t.transpose([1,0]) +[[1. 0.] + [0. 1.] + [0. 0.]] +``` + +`Tensor`变换,最多支持 6 维。 + +```python +>>> a = tensor.random((2,3,4,5,6,7)) +>>> a.shape +(2, 3, 4, 5, 6, 7) +>>> a.reshape((2,3,4,5,7,6)).transpose((3,2,1,0,4,5)).shape +(5, 4, 3, 2, 7, 6) +``` + +### Tensor 算术方法 + +`tensor`是实时计算的: + +```python +>>> t + 1 +[[2. 1. 1.] + [1. 2. 1.]] +>>> t / 5 +[[0.2 0. 0. ] + [0. 0.2 0. ]] +``` + +`tensor` broadcasting 运算: + +```python +>>> a +[[1. 2. 3.] + [4. 5. 6.]] +>>> b +[[1. 2. 3.]] +>>> a + b +[[2. 4. 6.] + [5. 7. 9.]] +>>> a * b +[[ 1. 4. 9.] + [ 4. 10. 18.]] +>>> a / b +[[1. 1. 1. ] + [4. 2.5 2. ]] +>>> a/=b # inplace operation +>>> a +[[1. 1. 1. ] + [4. 2.5 2. ]] +``` + +`tensor` broadcasting 矩阵乘法: + +```python +>>> from singa import tensor +>>> a = tensor.random((2,2,2,3)) +>>> b = tensor.random((2,3,4)) +>>> tensor.mult(a,b).shape +(2, 2, 2, 4) +``` + +### Tensor 函数 + +`singa.tensor`模块中的函数在应用函数中定义的变换后返回新的`Tensor`对象。 + +```python +>>> tensor.log(t+1) +[[0.6931472 0. 0. ] + [0. 0.6931472 0. ]] +``` + +### Tensor 在不同 Devices 上 + +`tensor`默认在主机(CPU)上创建;也可以通过指定设备在不同的硬件`device`上创建 +。`tensor`可以通过`to_device()`函数在`device`之间移动。 + +```python +>>> from singa import device +>>> x = tensor.Tensor((2, 3), device.create_cuda_gpu()) +>>> x.gaussian(1,1) +>>> x +[[1.531889 1.0128608 0.12691343] + [2.1674204 3.083676 2.7421203 ]] +>>> # move to host +>>> x.to_device(device.get_default_device()) +``` + +### 使用 Tensor 训练 MLP + +```python + +""" + code snipet from examples/mlp/module.py +""" + +label = get_label() +data = get_data() + +dev = device.create_cuda_gpu_on(0) +sgd = opt.SGD(0.05) + +# define tensor for input data and label +tx = tensor.Tensor((400, 2), dev, tensor.float32) +ty = tensor.Tensor((400,), dev, tensor.int32) +model = MLP(data_size=2, perceptron_size=3, num_classes=2) + +# attached model to graph +model.set_optimizer(sgd) +model.compile([tx], is_train=True, use_graph=True, sequential=False) +model.train() + +for i in range(1001): + tx.copy_from_numpy(data) + ty.copy_from_numpy(label) + out, loss = model(tx, ty, 'fp32', spars=None) + + if i % 100 == 0: + print("training loss = ", tensor.to_numpy(loss)[0]) +``` + +输出: + +```bash +$ python3 examples/mlp/module.py +training loss = 0.6158037 +training loss = 0.52852553 +training loss = 0.4571422 +training loss = 0.37274635 +training loss = 0.30146334 +training loss = 0.24906921 +training loss = 0.21128304 +training loss = 0.18390492 +training loss = 0.16362564 +training loss = 0.148164 +training loss = 0.13589878 +``` + +## Tensor 实现 + +上一节介绍了`Tensor`的一般用法,下面将介绍其底层的实现。首先,将介绍 Python 和 +C++ tensors 的设计。后面会讲到前端(Python)和后端(C++)如何连接,如何扩展。 + +### Python Tensor + +Python 类`Tensor`,定义在`python/singa/tensor.py`中,提供了高层的张量操作,用于 +实现深度学习操作(通过[autograd](./autograd)),以及终端用户的数据管理。 + +它主要是通过简单地封装 C++张量方法来工作,包括算术方法(如`sum`)和非算术方法( +如`reshape`)。一些高级的算术运算以后会引入,并使用纯 Python 的张量 API 来实现, +如`tensordot`。Python Tensor API 可以利用灵活的方法轻松实现复杂的神经网络操作。 + +### C++ Tensor + +C++类`Tensor`,定义在`include/singa/core/tensor.h`中,主要是管理存放数据的内存, +并提供低级的 API 用于张量操作。同时,它还通过封装不同的后端(CUDA、BLAS、cuBLAS +等)提供各种算术方法(如`matmul`)。 + +#### 执行背景和内存块 + +Tensor 的两个重要概念或者说数据结构是执行背景`device`,和内存块`Block`。 + +每个`Tensor`物理上存储在一个硬件设备上,并由硬件设备管理,代表执行背景 +(CPU、GPU),Tensor 的数学计算是在设备上执行的。 + +Tensor 数据在`Block`实例中,定义在`include/singa/core/common.h`中。`Block`拥有底 +层数据,而 tensors 则在描述 tensor 的元数据上拥有所有权,比如`shape`、`stride`。 + +#### Tensor 数学后端 + +为了利用不同后端硬件设备提供的高效数学库,SINGA 为每个支持的后端设备提供了一套 +Tensor 函数的实现。 + +- 'tensor_math_cpp.h'为 CppCPU 设备使用 Cpp(带 CBLAS)实现操作。 +- 'tensor_math_cuda.h'为 CudaGPU 设备实现了使用 Cuda(带 cuBLAS)的操作。 +- 'tensor_math_opencl.h'为 OpenclGPU 设备实现了使用 OpenCL 的操作。 + +### 将 C++ APIs 暴露给 Python + +SWIG(http://www.swig.org/)是一个可以自动将 C++ API 转换为 Python API 的工具 +。SINGA 使用 SWIG 将 C++ APIs 公开到 Python 中。SWIG 会生成几个文件,包 +括`python/singa/singa_wrap.py`。Python 模块(如`tensor`、`device`和`autograd`)导 +入这个模块来调用 C++ API 来实现 Python 类和函数。 + +```python +import tensor + +t = tensor.Tensor(shape=(2, 3)) +``` + +例如,当按上面的方法创建 Python `Tensor`实例时,`Tensor`类的实现会创建一个 +在`singa_wrap.py`中定义的 Tensor 类的实例,它对应于 C++ `Tensor`类。为了清楚起见 +,`singa_wrap.py`中的`Tensor`类在`tensor.py`中被称为`CTensor`。 + +```python +# in tensor.py +from . import singa_wrap as singa + +CTensor = singa.Tensor +``` + +### 创建新的 Tensor 函数 + +有了前面的描述所奠定的基础,扩展张量函数可以用自下而上的方式轻松完成。对于数学运 +算,其步骤是: + +- 在`tensor.h`中声明新的 API。 +- 使用 `tensor.cc` 中预定义的宏生成代码,参考 `GenUnaryTensorFn(Abs);` 。 +- 在`tensor_math.h`中声明 template 方法/函数。 +- 至少在 CPU(`tensor_math_cpp.h`)和 GPU(`tensor_math_cuda.h`)中进行真正的实现。 +- 将 API 加入到 `src/api/core_tensor.i` 中,通过 SWIG 公开 API。 +- 通过调用 `singa_wrap.py` 中自动生成的函数,在 `tensor.py` 中定义 Python Tensor + API。 +- 在适当的地方编写单元测试 + +## Python API + +_进行中_ + +## CPP API + +_进行中_ diff --git a/docs-site/website/versioned_docs/version-4.3.0_Chinese/time-profiling.md b/docs-site/website/versioned_docs/version-4.3.0_Chinese/time-profiling.md new file mode 100644 index 0000000..7c47386 --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0_Chinese/time-profiling.md @@ -0,0 +1,162 @@ +--- +id: version-4.2.0_Chinese-time-profiling +title: Time Profiling +original_id: time-profiling +--- + + + +SINGA 支持对图中缓冲的每个运算符进行时间分析。为了利用时间分析功能,我们首先调 +用`device.SetVerbosity`方法来设置时间分析器的 verbosity,然后调 +用`device.PrintTimeProfiling`来打印出时间分析的结果。 + +## 设置时间分析 Verbosity + +要使用时间分析功能,我们需要设置 verbosity。有三个级别的 verbosity。在默认 +值`verbosity == 0`的情况下,它不会进行任何时间分析。当我们设置`verbosity == 1`时 +,它将对前向和后向传播时间进行分析。当`verbosity == 2`时,它将对图中每个缓冲操作 +所花费的时间进行分析。 + +以下是设置时间分析功能的示例代码: + +```python +# create a device +from singa import device +dev = device.create_cuda_gpu() +# set the verbosity +verbosity = 2 +dev.SetVerbosity(verbosity) +# optional: skip the first 5 iterations when profiling the time +dev.SetSkipIteration(5) +``` + +那么,当我们在程序的最后完成训练后,我们就可以通过调 +用`device.PrintTimeProfiling`方法来打印时间分析结果。 + +```python +dev.PrintTimeProfiling() +``` + +## 不同 Verbosity 的输出示例 + +我们可以运行 ResNet +的[示例](https://github.com/apache/singa/blob/master/examples/cnn/benchmark.py), +看看不同的 verbosity 设置的输出。 + +1. `verbosity == 1` + +``` +Time Profiling: +Forward Propagation Time : 0.0409127 sec +Backward Propagation Time : 0.114813 sec +``` + +2. `verbosity == 2` + +``` +Time Profiling: +OP_ID0. SetValue : 1.73722e-05 sec +OP_ID1. cudnnConvForward : 0.000612724 sec +OP_ID2. GpuBatchNormForwardTraining : 0.000559449 sec +OP_ID3. ReLU : 0.000375004 sec +OP_ID4. GpuPoolingForward : 0.000240041 sec +OP_ID5. SetValue : 3.4176e-06 sec +OP_ID6. cudnnConvForward : 0.000115619 sec +OP_ID7. GpuBatchNormForwardTraining : 0.000150415 sec +OP_ID8. ReLU : 9.95494e-05 sec +OP_ID9. SetValue : 3.22432e-06 sec +OP_ID10. cudnnConvForward : 0.000648668 sec +OP_ID11. GpuBatchNormForwardTraining : 0.000149793 sec +OP_ID12. ReLU : 9.92118e-05 sec +OP_ID13. SetValue : 3.37728e-06 sec +OP_ID14. cudnnConvForward : 0.000400953 sec +OP_ID15. GpuBatchNormForwardTraining : 0.000572181 sec +OP_ID16. SetValue : 3.21312e-06 sec +OP_ID17. cudnnConvForward : 0.000398698 sec +OP_ID18. GpuBatchNormForwardTraining : 0.00056836 sec +OP_ID19. Add : 0.000542246 sec +OP_ID20. ReLU : 0.000372783 sec +OP_ID21. SetValue : 3.25312e-06 sec +OP_ID22. cudnnConvForward : 0.000260731 sec +OP_ID23. GpuBatchNormForwardTraining : 0.000149041 sec +OP_ID24. ReLU : 9.9072e-05 sec +OP_ID25. SetValue : 3.10592e-06 sec +OP_ID26. cudnnConvForward : 0.000637481 sec +OP_ID27. GpuBatchNormForwardTraining : 0.000152577 sec +OP_ID28. ReLU : 9.90518e-05 sec +OP_ID29. SetValue : 3.28224e-06 sec +OP_ID30. cudnnConvForward : 0.000404586 sec +OP_ID31. GpuBatchNormForwardTraining : 0.000569679 sec +OP_ID32. Add : 0.000542291 sec +OP_ID33. ReLU : 0.00037211 sec +OP_ID34. SetValue : 3.13696e-06 sec +OP_ID35. cudnnConvForward : 0.000261219 sec +OP_ID36. GpuBatchNormForwardTraining : 0.000148281 sec +OP_ID37. ReLU : 9.89299e-05 sec +OP_ID38. SetValue : 3.25216e-06 sec +OP_ID39. cudnnConvForward : 0.000633644 sec +OP_ID40. GpuBatchNormForwardTraining : 0.000150711 sec +OP_ID41. ReLU : 9.84902e-05 sec +OP_ID42. SetValue : 3.18176e-06 sec +OP_ID43. cudnnConvForward : 0.000402752 sec +OP_ID44. GpuBatchNormForwardTraining : 0.000571523 sec +OP_ID45. Add : 0.000542435 sec +OP_ID46. ReLU : 0.000372539 sec +OP_ID47. SetValue : 3.24672e-06 sec +OP_ID48. cudnnConvForward : 0.000493054 sec +OP_ID49. GpuBatchNormForwardTraining : 0.000293142 sec +OP_ID50. ReLU : 0.000190047 sec +OP_ID51. SetValue : 3.14784e-06 sec +OP_ID52. cudnnConvForward : 0.00148837 sec +OP_ID53. GpuBatchNormForwardTraining : 8.34794e-05 sec +OP_ID54. ReLU : 5.23254e-05 sec +OP_ID55. SetValue : 3.40096e-06 sec +OP_ID56. cudnnConvForward : 0.000292971 sec +OP_ID57. GpuBatchNormForwardTraining : 0.00029174 sec +OP_ID58. SetValue : 3.3248e-06 sec +OP_ID59. cudnnConvForward : 0.000590154 sec +OP_ID60. GpuBatchNormForwardTraining : 0.000294149 sec +OP_ID61. Add : 0.000275119 sec +OP_ID62. ReLU : 0.000189268 sec +OP_ID63. SetValue : 3.2704e-06 sec +OP_ID64. cudnnConvForward : 0.000341232 sec +OP_ID65. GpuBatchNormForwardTraining : 8.3304e-05 sec +OP_ID66. ReLU : 5.23667e-05 sec +OP_ID67. SetValue : 3.19936e-06 sec +OP_ID68. cudnnConvForward : 0.000542484 sec +OP_ID69. GpuBatchNormForwardTraining : 8.60537e-05 sec +OP_ID70. ReLU : 5.2479e-05 sec +OP_ID71. SetValue : 3.41824e-06 sec +OP_ID72. cudnnConvForward : 0.000291295 sec +OP_ID73. GpuBatchNormForwardTraining : 0.000292795 sec +OP_ID74. Add : 0.000274438 sec +OP_ID75. ReLU : 0.000189689 sec +OP_ID76. SetValue : 3.21984e-06 sec +OP_ID77. cudnnConvForward : 0.000338776 sec +OP_ID78. GpuBatchNormForwardTraining : 8.484e-05 sec +OP_ID79. ReLU : 5.29408e-05 sec +OP_ID80. SetValue : 3.18208e-06 sec +OP_ID81. cudnnConvForward : 0.000545542 sec +OP_ID82. GpuBatchNormForwardTraining : 8.40976e-05 sec +OP_ID83. ReLU : 5.2256e-05 sec +OP_ID84. SetValue : 3.36256e-06 sec +OP_ID85. cudnnConvForward : 0.000293003 sec +OP_ID86. GpuBatchNormForwardTraining : 0.0002989 sec +OP_ID87. Add : 0.000275041 sec +OP_ID88. ReLU : 0.000189867 sec +OP_ID89. SetValue : 3.1184e-06 sec +OP_ID90. cudnnConvForward : 0.000340417 sec +OP_ID91. GpuBatchNormForwardTraining : 8.39395e-05 sec +OP_ID92. ReLU : 5.26544e-05 sec +OP_ID93. SetValue : 3.2336e-06 sec +OP_ID94. cudnnConvForward : 0.000539787 sec +OP_ID95. GpuBatchNormForwardTraining : 8.2753e-05 sec +OP_ID96. ReLU : 4.86758e-05 sec +OP_ID97. SetValue : 3.24384e-06 sec +OP_ID98. cudnnConvForward : 0.000287108 sec +OP_ID99. GpuBatchNormForwardTraining : 0.000293127 sec +OP_ID100. Add : 0.000269478 sec +. +. +. +``` diff --git a/docs-site/website/versioned_docs/version-4.3.0_Chinese/wheel-cpu-dev.md b/docs-site/website/versioned_docs/version-4.3.0_Chinese/wheel-cpu-dev.md new file mode 100644 index 0000000..6cf4046 --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0_Chinese/wheel-cpu-dev.md @@ -0,0 +1,13 @@ +--- +id: version-4.2.0_Chinese-wheel-cpu-dev +title: CPU only (dev version) +original_id: wheel-cpu-dev +--- + + + +## 3.0.0.dev200720 + +- [Python 3.6](https://singa-wheel.s3-ap-southeast-1.amazonaws.com/singa-3.0.0.dev200720-cp36-cp36m-manylinux2014_x86_64.whl) +- [Python 3.7](https://singa-wheel.s3-ap-southeast-1.amazonaws.com/singa-3.0.0.dev200720-cp37-cp37m-manylinux2014_x86_64.whl) +- [Python 3.8](https://singa-wheel.s3-ap-southeast-1.amazonaws.com/singa-3.0.0.dev200720-cp38-cp38-manylinux2014_x86_64.whl) diff --git a/docs-site/website/versioned_docs/version-4.3.0_Chinese/wheel-cpu.md b/docs-site/website/versioned_docs/version-4.3.0_Chinese/wheel-cpu.md new file mode 100644 index 0000000..0b72ad6 --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0_Chinese/wheel-cpu.md @@ -0,0 +1,19 @@ +--- +id: version-4.2.0_Chinese-wheel-cpu +title: CPU only +original_id: wheel-cpu +--- + + + +## 3.1.0 + +- [Python 3.6](https://singa-wheel.s3-ap-southeast-1.amazonaws.com/singa-3.1.0-cp36-cp36m-manylinux2014_x86_64.whl) +- [Python 3.7](https://singa-wheel.s3-ap-southeast-1.amazonaws.com/singa-3.1.0-cp37-cp37m-manylinux2014_x86_64.whl) +- [Python 3.8](https://singa-wheel.s3-ap-southeast-1.amazonaws.com/singa-3.1.0-cp38-cp38-manylinux2014_x86_64.whl) + +## 3.0.0 + +- [Python 3.6](https://singa-wheel.s3-ap-southeast-1.amazonaws.com/singa-3.0.0-cp36-cp36m-manylinux2014_x86_64.whl) +- [Python 3.7](https://singa-wheel.s3-ap-southeast-1.amazonaws.com/singa-3.0.0-cp37-cp37m-manylinux2014_x86_64.whl) +- [Python 3.8](https://singa-wheel.s3-ap-southeast-1.amazonaws.com/singa-3.0.0-cp38-cp38-manylinux2014_x86_64.whl) diff --git a/docs-site/website/versioned_docs/version-4.3.0_Chinese/wheel-gpu-dev.md b/docs-site/website/versioned_docs/version-4.3.0_Chinese/wheel-gpu-dev.md new file mode 100644 index 0000000..69bd973 --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0_Chinese/wheel-gpu-dev.md @@ -0,0 +1,13 @@ +--- +id: version-4.2.0_Chinese-wheel-gpu-dev +title: CUDA enabled (dev version) +original_id: wheel-gpu-dev +--- + + + +## 3.0.0.dev200720 + +- [CUDA10.2, cuDNN 7.6.5, Python 3.6](https://singa-wheel.s3-ap-southeast-1.amazonaws.com/singa-3.0.0.dev200720%2Bcuda10.2-cp36-cp36m-manylinux2014_x86_64.whl) +- [CUDA10.2, cuDNN 7.6.5, Python 3.7](https://singa-wheel.s3-ap-southeast-1.amazonaws.com/singa-3.0.0.dev200720%2Bcuda10.2-cp37-cp37m-manylinux2014_x86_64.whl) +- [CUDA10.2, cuDNN 7.6.5, Python 3.8](https://singa-wheel.s3-ap-southeast-1.amazonaws.com/singa-3.0.0.dev200720%2Bcuda10.2-cp38-cp38-manylinux2014_x86_64.whl) diff --git a/docs-site/website/versioned_docs/version-4.3.0_Chinese/wheel-gpu.md b/docs-site/website/versioned_docs/version-4.3.0_Chinese/wheel-gpu.md new file mode 100644 index 0000000..f297d05 --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0_Chinese/wheel-gpu.md @@ -0,0 +1,22 @@ +--- +id: version-4.2.0_Chinese-wheel-gpu +title: CUDA enabled +original_id: wheel-gpu +--- + + + +## 3.1.0 + +- [CUDA10.2, cuDNN 7.6.5, Python + 3.6]https://singa-wheel.s3-ap-southeast-1.amazonaws.com/singa-3.1.0%2Bcuda10.2-cp36-cp36m-manylinux2014_x86_64.whl) +- [CUDA10.2, cuDNN 7.6.5, Python + 3.7]https://singa-wheel.s3-ap-southeast-1.amazonaws.com/singa-3.1.0%2Bcuda10.2-cp37-cp37m-manylinux2014_x86_64.whl) +- [CUDA10.2, cuDNN 7.6.5, Python + 3.8]https://singa-wheel.s3-ap-southeast-1.amazonaws.com/singa-3.1.0%2Bcuda10.2-cp38-cp38-manylinux2014_x86_64.whl) + +## 3.0.0 + +- [CUDA10.2, cuDNN 7.6.5, Python 3.6](https://singa-wheel.s3-ap-southeast-1.amazonaws.com/singa-3.0.0%2Bcuda10.2-cp36-cp36m-manylinux2014_x86_64.whl) +- [CUDA10.2, cuDNN 7.6.5, Python 3.7](https://singa-wheel.s3-ap-southeast-1.amazonaws.com/singa-3.0.0%2Bcuda10.2-cp37-cp37m-manylinux2014_x86_64.whl) +- [CUDA10.2, cuDNN 7.6.5, Python 3.8](https://singa-wheel.s3-ap-southeast-1.amazonaws.com/singa-3.0.0%2Bcuda10.2-cp38-cp38-manylinux2014_x86_64.whl) diff --git a/docs-site/website/versioned_docs/version-4.3.0_Viet/autograd.md b/docs-site/website/versioned_docs/version-4.3.0_Viet/autograd.md new file mode 100644 index 0000000..36eb99a --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0_Viet/autograd.md @@ -0,0 +1,282 @@ +--- +id: version-4.2.0_Viet-autograd +title: Autograd +original_id: autograd +--- + + + +Có hai cách thường dùng để sử dụng autograd, qua symbolic differentiation như là +[Theano](http://deeplearning.net/software/theano/index.html) hoặc reverse +differentiation như là +[Pytorch](https://pytorch.org/docs/stable/notes/autograd.html). SINGA dùng cách +Pytorch, lưu trữ computation graph rồi áp dụng backward propagation tự động sau +khi forward propagation. Thuật toán autograd được giải thích cụ thể ở +[đây](https://pytorch.org/docs/stable/notes/autograd.html). Chúng tôi giải thích +các modules liên quan trong Singa và đưa ra ví dụ để minh hoạ cách sử dụng. + +## Các Module liên quan + +Autograd gồm ba classes với tên gọi `singa.tensor.Tensor`, +`singa.autograd.Operation`, và `singa.autograd.Layer`. Trong phần tiếp theo của +văn kiện này, chúng tôi dùng tensor, operation và layer để chỉ một chương trình +(instance) trong class tương ứng. + +### Tensor + +Ba tính năng của Tensor được sử dụng bởi autograd, + +- `.creator` là một chương trình `Operation`. Chương trình này lưu trữ tác vụ + tạo ra Tensor instance. +- `.requires_grad` là một biến kiểu bool. Biến được sử dụng để chỉ rằng thuật + toán autograd cần tính ra độ dốc (gradient) của tensor. (như owner). Ví dụ, + khi chạy backpropagation, thì cần phải tính ra độ dốc của tensor cho ma trận + trọng lượng (weight matrix) của lớp tuyến tính (linear layer) và bản đồ tính + năng (feature map) của convolution layer (không phải lớp cuối). +- `.stores_grad` là một biến kiểu bool. Biến được sử dụng để chỉ rằng độ dốc của + owner tensor cần được lưu và tạo ra bởi hàm backward. Ví dụ, độ dốc của + feature maps được tính thông qua backpropagation, nhưng không được bao gồm + trong kết quả của hàm backward. + +Lập trình viên có thể thay đổi `requires_grad` và `stores_grad` của chương trình +Tensor. Ví dụ nếu hàm sau để là True, độ dốc tương ứng sẽ được bao gồm trong kết +quả của hàm backward. Cần lưu ý rằng nếu `stores_grad` để là True, thì +`requires_grad` cũng phải là True, và ngược lại. + +### Operation + +Hàm chạy một hoặc một vài chương trình `Tensor` instances ở đầu vào, sau đó đầu +ra là một hoặc một vài chương trình `Tensor` instances. Ví dụ, hàm ReLU có thể +được sử dụng như một subclass của một hàm Operation cụ thể. Khi gọi một chương +trình `Operation` (sau cài đặt), cần thực hiện hai bước sau: + +1. Ghi lại hàm operations nguồn, vd. biến `creator`của tensor đầu vào. +2. làm tính toán bằng cách gọi hàm thành viên `.forward()` + +Có hai hàm thành viên cho forwarding và backwarding, vd. `.forward()` và +`.backward()`. Đầu vào là `Tensor.data` (thuộc loại `CTensor`), và đầu ra là +`Ctensor`. Nếu muốn thêm một hàm operation thì subclass `operation` cần chạy +riêng `.forward()` và `.backward()`. Hàm `backward()` được tự động gọi bởi hàm +`backward()` của autograd trong quá trình chạy backward để thực hiện độ dốc của +đầu vào (theo mục `require_grad`). + +### Layer + +Với các hàm yêu cầu tham số (parameter), chúng tôi gói chúng lại thành một class +mới, `Layer`. Ví dụ hàm convolution operation thì được nhóm vào trong +convolution layer. `Layer` quản lý (hoặc lưu trữ) các tham số và sẽ gọi các hàm +`Operation` tương ứng để thực hiện việc chuyển đổi. + +## Ví dụ + +Chúng tôi cung cấp nhiều ví dụ trong +[mục ví dụ](https://github.com/apache/singa/tree/master/examples/autograd). +Chúng tôi đưa ra giải thích cụ thể trong hai ví dụ tiêu biểu ở đây. + +### Dùng hàm Operation + +Code dưới đây áp dụng model MLP, chỉ dùng hàm Operation (không dùng hàm Layer). + +#### Thêm packages + +```python +from singa.tensor import Tensor +from singa import autograd +from singa import opt +``` + +#### Tạo ma trận trọng lượng (weight matrix) và bias vector + +Tham số tensors được tạo bởi cả `requires_grad` và `stores_grad` ở giá trị +`True`. + +```python +w0 = Tensor(shape=(2, 3), requires_grad=True, stores_grad=True) +w0.gaussian(0.0, 0.1) +b0 = Tensor(shape=(1, 3), requires_grad=True, stores_grad=True) +b0.set_value(0.0) + +w1 = Tensor(shape=(3, 2), requires_grad=True, stores_grad=True) +w1.gaussian(0.0, 0.1) +b1 = Tensor(shape=(1, 2), requires_grad=True, stores_grad=True) +b1.set_value(0.0) +``` + +#### Training + +```python +inputs = Tensor(data=data) # data matrix +target = Tensor(data=label) # label vector +autograd.training = True # cho training +sgd = opt.SGD(0.05) # optimizer + +for i in range(10): + x = autograd.matmul(inputs, w0) # matrix multiplication + x = autograd.add_bias(x, b0) # add the bias vector + x = autograd.relu(x) # ReLU activation operation + + x = autograd.matmul(x, w1) + x = autograd.add_bias(x, b1) + + loss = autograd.softmax_cross_entropy(x, target) + + for p, g in autograd.backward(loss): + sgd.update(p, g) +``` + +### Hàm Operation + Layer + +[Ví dụ](https://github.com/apache/singa/blob/master/examples/autograd/mnist_cnn.py) +sau đây áp dụng CNN model sử dụng các lớp (layers) tạo từ autograd module. + +#### Tạo layers + +```python +conv1 = autograd.Conv2d(1, 32, 3, padding=1, bias=False) +bn1 = autograd.BatchNorm2d(32) +pooling1 = autograd.MaxPool2d(3, 1, padding=1) +conv21 = autograd.Conv2d(32, 16, 3, padding=1) +conv22 = autograd.Conv2d(32, 16, 3, padding=1) +bn2 = autograd.BatchNorm2d(32) +linear = autograd.Linear(32 * 28 * 28, 10) +pooling2 = autograd.AvgPool2d(3, 1, padding=1) +``` + +#### Định nghĩa hàm forward + +Hàm trong forward pass sẽ được tự đông lưu cho backward propagation. + +```python +def forward(x, t): + # x là input data (batch hình ảnh) + # t là label vector (batch số nguyên) + y = conv1(x) # Conv layer + y = autograd.relu(y) # ReLU operation + y = bn1(y) # BN layer + y = pooling1(y) # Pooling Layer + + # hai convolution layers song song + y1 = conv21(y) + y2 = conv22(y) + y = autograd.cat((y1, y2), 1) # cat operation + y = autograd.relu(y) # ReLU operation + y = bn2(y) + y = pooling2(y) + + y = autograd.flatten(y) # flatten operation + y = linear(y) # Linear layer + loss = autograd.softmax_cross_entropy(y, t) # operation + return loss, y +``` + +#### Training + +```python +autograd.training = True +for epoch in range(epochs): + for i in range(batch_number): + inputs = tensor.Tensor(device=dev, data=x_train[ + i * batch_sz:(1 + i) * batch_sz], stores_grad=False) + targets = tensor.Tensor(device=dev, data=y_train[ + i * batch_sz:(1 + i) * batch_sz], requires_grad=False, stores_grad=False) + + loss, y = forward(inputs, targets) # forward the net + + for p, gp in autograd.backward(loss): # auto backward + sgd.update(p, gp) +``` + +### Sử dụng Model API + +[Ví dụ](https://github.com/apache/singa/blob/master/examples/cnn/model/cnn.py) +sau áp dụng CNN model sử dụng [Model API](./graph). + +#### Định nghiã subclass của Model + +Model class được định nghĩa là subclass của Model. Theo đó, tất cả các hàm +operations được sử dụng trong bước training sẽ tạo thành một computational graph +và được phân tích. Hàm operation trong graph sẽ được lên lịch trình và thực hiện +hiệu quả. Layers cũng có thể được bao gồm trong model class. + +```python +class MLP(model.Model): # model là subclass của Model + + def __init__(self, data_size=10, perceptron_size=100, num_classes=10): + super(MLP, self).__init__() + + # taọ operators, layers và các object khác + self.relu = layer.ReLU() + self.linear1 = layer.Linear(perceptron_size) + self.linear2 = layer.Linear(num_classes) + self.softmax_cross_entropy = layer.SoftMaxCrossEntropy() + + def forward(self, inputs): # định nghĩa forward function + y = self.linear1(inputs) + y = self.relu(y) + y = self.linear2(y) + return y + + def train_one_batch(self, x, y): + out = self.forward(x) + loss = self.softmax_cross_entropy(out, y) + self.optimizer(loss) + return out, loss + + def set_optimizer(self, optimizer): # đính kèm optimizer + self.optimizer = optimizer +``` + +#### Training + +```python +# tạo hàm model instance +model = MLP() +# tạo optimizer và đính vào model +sgd = opt.SGD(lr=0.005, momentum=0.9, weight_decay=1e-5) +model.set_optimizer(sgd) +# input và target placeholders cho model +tx = tensor.Tensor((batch_size, 1, IMG_SIZE, IMG_SIZE), dev, tensor.float32) +ty = tensor.Tensor((batch_size, num_classes), dev, tensor.int32) +# tổng hợp model trước khi training +model.compile([tx], is_train=True, use_graph=True, sequential=False) + +# train model theo bước lặp (iterative) +for b in range(num_train_batch): + # generate the next mini-batch + x, y = ... + + # Copy the data into input tensors + tx.copy_from_numpy(x) + ty.copy_from_numpy(y) + + # Training with one batch + out, loss = model(tx, ty) +``` + +#### Lưu model checkpoint + +```python +# xác định đường dẫn để lưu checkpoint +checkpointpath="checkpoint.zip" + +# lưu checkpoint +model.save_states(fpath=checkpointpath) +``` + +#### Tải model checkpoint + +```python +# xác định đường dẫn để lưu checkpoint +checkpointpath="checkpoint.zip" + +# lưu checkpoint +import os +if os.path.exists(checkpointpath): + model.load_states(fpath=checkpointpath) +``` + +### Python API + +Xem +[tại đây](https://singa.readthedocs.io/en/latest/autograd.html#module-singa.autograd) +để thêm thông tin chi tiết về Python API. diff --git a/docs-site/website/versioned_docs/version-4.3.0_Viet/benchmark-train.md b/docs-site/website/versioned_docs/version-4.3.0_Viet/benchmark-train.md new file mode 100644 index 0000000..ac7852e --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0_Viet/benchmark-train.md @@ -0,0 +1,29 @@ +--- +id: version-4.2.0_Viet-benchmark-train +title: Benchmark cho Distributed Training +original_id: benchmark-train +--- + + + +Tải công việc: chúng tôi sử dụng Mạng nơ-ron tích chập sâu (deep convolutional +neural network), +[ResNet-50](https://github.com/apache/singa/blob/master/examples/cnn/model/resnet.py) +làm ứng dụng. ResNet-50 có 50 lớp tích chập (convolution layers) để phân loại +hình ảnh. Nó đòi hỏi 3.8 GFLOPs để đưa vào một hình ảnh (kích thước ảnh 224x224) +qua mạng lưới. Kích thước ảnh đầu vào là 224x224. + +Phần cứng: chúng tôi sử dụng máy p2.8xlarge từ AWS, mỗi máy gồm 8 Nvidia Tesla +K80 GPUs, bộ nhớ tổng cộng 96 GB GPU, 32 vCPU, 488 GB main memory, 10 Gbps +network bandwidth. + +Metric: chúng tôi tính thời gian mỗi bước cho mỗi workers để đánh giá khả năng +mở rộng của SINGA. Kích thước của mỗi nhóm được cố định ở 32 mỗi GPU. Phương +thức training đồng bộ (Synchronous training scheme) được áp dụng. Vì thế, kích +thước nhóm hiệu quả là $32N$, trong đó N là số máy GPUs. Chúng tôi so sánh với +một hệ thống mở được dùng phổ biến có sử dụng tham số server cấu trúc liên kết. +Máy GPU đầu tiên được chọn làm server. + +![Thí nghiệm Benchmark](assets/benchmark.png)
**Kiểm tra khả năng mở rộng. +Bars được dùng cho thông lượng (throughput); lines dùng cho lượng kết nối +(communication cost).** diff --git a/docs-site/website/versioned_docs/version-4.3.0_Viet/build.md b/docs-site/website/versioned_docs/version-4.3.0_Viet/build.md new file mode 100644 index 0000000..db6510b --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0_Viet/build.md @@ -0,0 +1,523 @@ +--- +id: version-4.2.0_Viet-build +title: Cài đặt SINGA từ Nguồn (Source) +original_id: build +--- + + + +Các tệp nguồn có thể được tải dưới dạng +[tar.gz file](https://dist.apache.org/repos/dist/dev/singa/), hoặc git repo + +```shell +$ git clone https://github.com/apache/singa.git +$ cd singa/ +``` + +Nếu bạn muốn tham gia đóng góp code cho SINGA, tham khảo +[mục contribute-code](contribute-code.md) với các bước làm và yêu cầu kĩ thuật. + +## Sử dụng Conda để cài SINGA + +Conda-build là phần mềm giúp cài đặt thư viện chương trình từ dữ liệu đám mây +anaconda và thực hiện các tập lệnh tạo chương trình. + +Để cài đặt conda-build (sau khi cài conda) + +```shell +conda install conda-build +``` + +### Tạo phiên bản CPU + +Để tạo phiên bản CPU cho SINGA + +```shell +conda build tool/conda/singa/ +``` + +Lệnh trên đã được kiểm tra trên Ubuntu (14.04, 16.04 và 18.04) và macOS 10.11. +Tham khảo [trang Travis-CI](https://travis-ci.org/apache/singa) để biết thêm chi +tiết. + +### Tạo phiên bản GPU + +Để tạo phiên bản GPU cho SINGA, máy để cài phải có Nvida GPU, và CUDA driver (>= +384.81), phải được cài đặt CUDA toolkit (>=9) và cuDNN (>=7). Hai Docker images +dưới đây cung cấp environment để chạy: + +1. apache/singa:conda-cuda9.0 +2. apache/singa:conda-cuda10.0 + +Sau khi environment để chạy đã sẵn sàng, bạn cần phải export phiên bản CUDA +trước, sau đó chạy lệnh conda để cài SINGA: + +```shell +export CUDA=x.y (e.g. 9.0) +conda build tool/conda/singa/ +``` + +### Sau khi chạy chương trình + +Vị trí đặt tệp tin của gói chương trình được tạo (`.tar.gz`) hiển thị trên màn +hình. Gói chương trình được tạo có thể được cài đặt trực tiếp, + +```shell +conda install -c conda-forge --use-local +``` + +hoặc tải lên dữ liệu đám mây anaconda cloud để người dùng khác có thể tải và cài +đặt. Bạn cần phải đăng kí một tài khoản trên anaconda để có thể +[tải lên gói chương trình](https://docs.anaconda.com/anaconda-cloud/user-guide/getting-started/). + +```shell +conda install anaconda-client +anaconda login +anaconda upload -l main +``` + +Sau khi tải gói chương trình lên dữ liệu đám mây, bạn có thể tìm thấy gói trên +website của [Anaconda Cloud](https://anaconda.org/) hoặc qua lệnh + +```shell +conda search -c singa +``` + +Mỗi gói chương trình của SINGA đuợc nhận diện theo phiên bản hoặc dòng lệnh cài +đặt. Để cài một gói chương trình SINGA cụ thể, bạn cần phải cung cấp toàn bộ +thông tin, vd. + +```shell +conda install -c -c conda-forge singa=2.1.0.dev=cpu_py36 +``` + +Để cho lệnh cài đặt không phức tạp, bạn có thể tạo các gói chương trình bổ sung +sau dựa trên các gói chương trình cho SINGA CPU và GPU mới nhất . + +```console +# for singa-cpu +conda build tool/conda/cpu/ --python=3.6 +conda build tool/conda/cpu/ --python=3.7 +# for singa-gpu +conda build tool/conda/gpu/ --python=3.6 +conda build tool/conda/gpu/ --python=3.7 +``` + +Bởi vậy, khi bạn chạy + +```shell +conda install -c -c conda-forge singa-xpu +``` + +(`xpu` nghĩa là hoặc 'cpu' hoặc 'gpu'), gói SINGA tương ứng thực sự được cài đặt +như một library phụ thuộc. + +## Sử dụng các phương tiện cơ bản để cài đặt SINGA trên Ubuntu + +Tham khảo +[Dockerfiles](https://github.com/apache/singa/blob/master/tool/docker/devel/ubuntu/cuda9/Dockerfile#L30) +của SINGA để xem hướng dẫn cài đặt các chương trình library phụ thuộc trên +Ubuntu 16.04. Bạn có thể tạo một Docker container sử dụng [devel images]() và +cài SINGA trong container. Để cài SINGA với GPU, DNNL, Python và unit tests, +chạy lệnh theo hướng dẫn sau + +```shell +mkdir build # tại thư mục nguồn của singa +cd build +cmake -DENABLE_TEST=ON -DUSE_CUDA=ON -DUSE_DNNL=ON -DUSE_PYTHON3=ON .. +make +cd python +pip install . +``` + +Chi tiết các lựa chọn CMake đuợc giải thích ở phần cuối cùng của trang này. Câu +lệnh cuối cùng để cài gói Python. Bạn cúng có thể chạy `pip install -e .`, để +tạo symlinks thay vì copy các tâp tin Python vào mục site-package. + +Nếu SINGA được compile với ENABLE_TEST=ON, bạn có thể chạy unit test bằng cách + +```shell +$ ./bin/test_singa +``` + +Bạn sẽ thấy tất cả các trường hợp test kèm theo kết quả test. Nếu SINGA thông +qua tất cả các test, bạn đã cài đặt SINGA thành công. + +## Sử dụng các phương tiện cơ bản để cài đặt SINGA trên Centos7 + +Tạo từ nguồn sẽ khác trên Centos7 bởi tên của gói chương trình là khác nhau. Làm +theo hướng dẫn dưới đây + +### Cài các chương trình phụ thuộc (dependent libraries) + +Gói/chương trình cơ bản + +```shell +sudo yum install freetype-devel libXft-devel ncurses-devel openblas-devel blas-devel lapack devel atlas-devel kernel-headers unzip wget pkgconfig zip zlib-devel libcurl-devel cmake curl unzip dh-autoreconf git python-devel glog-devel protobuf-devel +``` + +Cho build-essential + +```shell +sudo yum group install "Development Tools" +``` + +Để cài đặt swig + +```shell +sudo yum install pcre-devel +wget http://prdownloads.sourceforge.net/swig/swig-3.0.10.tar.gz +tar xvzf swig-3.0.10.tar.gz +cd swig-3.0.10.tar.gz +./configure --prefix=${RUN} +make +make install +``` + +Để cài đặt gfortran + +```shell +sudo yum install centos-release-scl-rh +sudo yum --enablerepo=centos-sclo-rh-testing install devtoolset-7-gcc-gfortran +``` + +Để cài đặt pip và các gói chương trình khác + +```shell +sudo yum install epel-release +sudo yum install python-pip +pip install matplotlib numpy pandas scikit-learn pydot +``` + +### Cài đặt + +Làm theo bước 1-5 của _Use native tools để cài SINGA trên Ubuntu_ + +### Kiểm tra (testing) + +Bạn có thể chạy unit tests bằng cách, + +```shell +$ ./bin/test_singa +``` + +Bạn sẽ thấy tất cả các trường hợp test kèm theo kết quả test. Nếu SINGA thông +qua tất cả các test, bạn đã cài đặt SINGA thành công. + +## Compile SINGA trên Windows + +Hướng dẫn cài đặt trên Windows với Python vui lòng xem tại +[mục install-win](install-win.md). + +## Chi tiết bổ sung về các lựa chọn biên dịch (compilation) + +### USE_MODULES (không còn sử dụng) + +Nếu protobuf và openblas không được cài đặt, bạn có thể compile SINGA cùng với +chúng. + +```shell +$ In SINGA ROOT folder +$ mkdir build +$ cd build +$ cmake -DUSE_MODULES=ON .. +$ make +``` + +cmake sẽ tải OpenBlas và Protobuf (2.6.1) sau đó compile cùng với SINGA. + +Bạn có thể sử dụng `ccmake ..` để định dạng các lựa chọn biên dịch +(compilation). Nếu chương trình phụ thuộc (dependent libraries) nào không có +trong đường dẫn hệ thống mặc định,bạn cần phải export các biến environment sau: + +```shell +export CMAKE_INCLUDE_PATH= +export CMAKE_LIBRARY_PATH= +``` + +### USE_PYTHON + +Là lựa chọn để compile Python wrapper cho SINGA, + +```shell +$ cmake -DUSE_PYTHON=ON .. +$ make +$ cd python +$ pip install . +``` + +### USE_CUDA + +Chúng tôi khuyến khích cài đặt CUDA và +[cuDNN](https://developer.nvidia.com/cudnn) để chạy SINGA trên GPUs nhằm có kết +quả tốt nhất. + +SINGA đã được kiểm nghiệm chạy trên CUDA 9/10, và cuDNN 7. Nếu cuDNN được cài +đặt vào thư mục không thuộc hệ thống, vd. /home/bob/local/cudnn/, cần chạy các +lệnh sau để cmake và runtime có thể tìm được + +```shell +$ export CMAKE_INCLUDE_PATH=/home/bob/local/cudnn/include:$CMAKE_INCLUDE_PATH +$ export CMAKE_LIBRARY_PATH=/home/bob/local/cudnn/lib64:$CMAKE_LIBRARY_PATH +$ export LD_LIBRARY_PATH=/home/bob/local/cudnn/lib64:$LD_LIBRARY_PATH +``` + +Các lựa chọn cmake cho CUDA và cuDNN cần được kích hoạt + +```shell +# các Dependent libs đã được cài đặt +$ cmake -DUSE_CUDA=ON .. +$ make +``` + +### USE_DNNL + +Người dùng có thể kích hoạt DNNL để cải thiện hiệu quả cho chương trình CPU. + +Hướng dẫn cài đặt DNNL [tại đây](https://github.com/intel/mkl-dnn#installation). + +SINGA đã được thử nghiệm chạy trên DNNL v1.1. + +Để chạy SINGA với DNNL: + +```shell +# các Dependent libs đã được cài đặt +$ cmake -DUSE_DNNL=ON .. +$ make +``` + +### USE_OPENCL + +SINGA sử dụng opencl-headers và viennacl (phiên bản 1.7.1 hoặc mới hơn) để hỗ +trợ OpenCL, có thể được cài đặt qua + +```shell +# Trên Ubuntu 16.04 +$ sudo apt-get install opencl-headers, libviennacl-dev +# Trên Fedora +$ sudo yum install opencl-headers, viennacl +``` + +Bên cạnh đó, bạn cần OpenCL Installable Client Driver (ICD) cho nền tảng mà bạn +muốn chạy OpenCL. + +- Với AMD và nVidia GPUs, driver package nên cài đúng bản OpenCL ICD. +- Với Intel CPUs và/hoặc GPUs, có thể tải driver từ + [Intel website.](https://software.intel.com/en-us/articles/opencl-drivers) Lưu + ý rằng driver này chỉ hỗ trợ các phiên bản mới của CPUs và Iris GPUs. +- Với các bản Intel CPUs cũ hơn, bạn có thể sử dụng gói `beignet-opencl-icd`. + +Lưu ý rằng chạy OpenCL trên CPUs không được khuyến khích bởi tốc độ chậm. Di +chuyển bộ nhớ theo trình tự tính theo từng giây (1000's của ms trên CPUs so với +1's của ms trên GPUs). + +Có thể xem thêm thông tin về cách thiết lập environment có chạy OpenCL tại +[đây](https://wiki.tiker.net/OpenCLHowTo). + +Nếu phiên bản của gói chương trình ViennaCL thấp hơn 1.7.1, bạn cần phải tạo từ +nguồn: + +Clone [nguồn tại đây](https://github.com/viennacl/viennacl-dev), chọn (checkout) +tag `release-1.7.1` để cài đặt. Bạn cần nhớ thêm đường dẫn vào phần `PATH` và +tạo libraries vào `LD_LIBRARY_PATH`. + +Để cài SINGA với hỗ trợ OpenCL (đã thử trên SINGA 1.1): + +```shell +$ cmake -DUSE_OPENCL=ON .. +$ make +``` + +### GÓI CHƯƠNG TRÌNH (PACKAGE) + +Cài đặt này được sử dụng để tạo gói chương trình Debian package. Để PACKAGE=ON +và tạo gói chương trình với lệnh như sau: + +```shell +$ cmake -DPACKAGE=ON +$ make package +``` + +## Câu hỏi thường gặp (Q&A) + +- Q: Gặp lỗi khi 'import singa' + + A: Vui lòng kiểm tra chi tiết lỗi từ + `python -c "from singa import _singa_wrap"`. Đôi khi lỗi xảy ra bởi các + dependent libraries, vd. protobuf có nhiều phiên bản, nếu thiếu cudnn, phiên + bản numpy sẽ không tương thích. Các bước sau đưa ra giải pháp cho từng trường + hợp: + + 1. Kiểm tra cudnn và cuda. Nếu thiếu cudnn hoặc không tương thích với phiên + bản của wheel, bạn có thể tải phiên bản đúng của cudnn vào thư mục + ~/local/cudnn/ và + + ```shell + $ echo "export LD_LIBRARY_PATH=/home//local/cudnn/lib64:$LD_LIBRARY_PATH" >> ~/.bashrc + ``` + + 2. Nếu lỗi liên quan tới protobuf. Bạn có thể cài đặt (3.6.1) từ nguồn vào một + thư mục trong máy của bạn(local). chẳng hạn ~/local/; Giải nén file tar, + sau đó + + ```shell + $ ./configure --prefix=/home/local + $ make && make install + $ echo "export LD_LIBRARY_PATH=/home//local/lib:$LD_LIBRARY_PATH" >> ~/.bashrc + $ source ~/.bashrc + ``` + + 3. Nếu không tìm được libs nào bao gồm python, thì taọ virtual env sử dụng + `pip` hoặc `conda`; + + 4. Nếu lỗi không do các nguyên nhân trên thì đi tới thư mục của + `_singa_wrap.so`, + + ```shell + $ python + >> import importlib + >> importlib.import_module('_singa_wrap') + ``` + + kiểm tra thông báo lỗi. Ví dụ nếu phiên bản numpy không tương thích, thông báo + lỗi sẽ là + + ```shell + RuntimeError: module compiled against API version 0xb but this version of numpy is 0xa + ``` + + sau đó bạn cần phải nâng cấp numpy. + +* Q: Lỗi khi chạy `cmake ..`, không tìm được dependent libraries. + + A: Nếu bạn vẫn chưa cài đặt libraries đó, thì cài đặt chúng. Nếu bạn cài + libraries trong thư mục bên ngoài thư mục system, chẳng hạn như /usr/local, + bạn cần export các biến sau đây + + ```shell + $ export CMAKE_INCLUDE_PATH= + $ export CMAKE_LIBRARY_PATH= + ``` + +- Q: Lỗi từ `make`, vd. linking phase + + A: Nếu libraries nằm trong thư mục không phải là thư mục system mặc định trong + đường dẫn, bạn cần export các biến sau + + ```shell + $ export LIBRARY_PATH= + $ export LD_LIBRARY_PATH= + ``` + +* Q: Lỗi từ các tệp tin headers vd. 'cblas.h no such file or directory exists' + + A: Bạn cần bao gồm các thư mục cblas.h vào CPLUS_INCLUDE_PATH, e.g., + + ```shell + $ export CPLUS_INCLUDE_PATH=/opt/OpenBLAS/include:$CPLUS_INCLUDE_PATH + ``` + +* Q: Khi compile SINGA, gặp lỗi `SSE2 instruction set not enabled` + + A: Bạn có thể thử lệnh sau: + + ```shell + $ make CFLAGS='-msse2' CXXFLAGS='-msse2' + ``` + +* Q:Gặp lỗi `ImportError: cannot import name enum_type_wrapper` từ + google.protobuf.internal khi tôi cố gắng import các tệp tin dạng .py. + + A: Bạn cần cài đặt python cho protobuf, có thể cài đặt qua + + ```shell + $ sudo apt-get install protobuf + ``` + +hoặc từ nguồn + +```shell +$ cd /PROTOBUF/SOURCE/FOLDER +$ cd python +$ python setup.py build +$ python setup.py install +``` + +- Q: Khi tôi tạo OpenBLAS từ nguồn, tôi gặp yêu cầu cần phải có Fortran + compiler. + + A: Bạn có thể compile OpenBLAS bằng cách + + ```shell + $ make ONLY_CBLAS=1 + ``` + + hoặc cài dặt sử dụng + + ```shell + $ sudo apt-get install libopenblas-dev + ``` + +- Q: Khi tôi tạo protocol buffer, thì bị thông báo `GLIBC++_3.4.20` không được + tìm thấy trong `/usr/lib64/libstdc++.so.6`? + + A: Nghĩa là linker trong libstdc++.so.6 nhưng chương trình này thuộc về một + phiên bản cũ hơn của GCC đã được dùng để compile và link chương trình. Chương + trình phụ thuộc vào code viết trong phiên bản libstdc++ cập nhật thuộc về + phiên bản mới hơn của GCC, vì vậy linker phải chỉ ra cách để cài phiên bản + libstdc++ mới hơn được chia sẻ trong library. Cách đơn giản nhất để sửa lỗi + này là tìm phiên bản đúng cho libstdc++ và export nó vào LD_LIBRARY_PATH. Ví + dụ nếu GLIBC++\_3.4.20 có trong output của lệnh sau + + $ strings /usr/local/lib64/libstdc++.so.6|grep GLIBC++ + + thì bạn chỉ cần tạo biến environment + + $ export LD_LIBRARY_PATH=/usr/local/lib64:$LD_LIBRARY_PATH + +- Q: Khi tạo glog, nhận thông báo "src/logging_unittest.cc:83:20: error: + ‘gflags’ is not a namespace-name" + + A: Có thể do bạn đã cài gflags với một namespace khác như là "google". vì thế + glog không thể tìm thấy 'gflags' namespace. Do cài glog thì không cần phải có + gflags. Nên bạn cần sửa tệp tin configure.ac thành ignore gflags. + + 1. cd to glog src directory + 2. change line 125 of configure.ac to "AC_CHECK_LIB(gflags, main, ac_cv_have_libgflags=0, ac_cv_have_libgflags=0)" + 3. autoreconf + + Sau đó bạn có thể cài lại glog. + +- Q: Khi sử dụng virtual environment, bất cứ khi nào tôi chạy pip install, numpy + sẽ tự cài lại numpy. Tuy nhiên, numpy này không được sử dụng khi tôi + `import numpy` + + A: Lỗi có thể gây ra bởi `PYTHONPATH` vốn nên được để trống (empty) khi bạn sử + dụng virtual environment nhằm tránh conflict với đường dẫn của virtual + environment. + +- Q: Khi compile PySINGA từ nguồn, có lỗi compilation do thiếu + + + A: Vui lòng cài đặt numpy và export đường dẫn của tệp tin numpy header như sau + + $ export CPLUS_INCLUDE_PATH=`python -c "import numpy; print numpy.get_include()"`:$CPLUS_INCLUDE_PATH + +- Q: Khi chạy SINGA trên Mac OS X, tôi gặp lỗi "Fatal Python error: + PyThreadState_Get: no current thread Abort trap: 6" + + A: Lỗi này thường xảy ra khi bạn có nhiều phiên bản Python trong hệ thống, và + bạn cài SINGA qua pip (vấn đề này có thể được giải quyết nếu cài đặt bằng + conda), vd. một bên qua OS và một bên cài đặt qua Homebrew. Python dùng trong + PySINGA phải là Python interpreter. Bạn có thể kiểm tra trình thông dịch + (interpreter) của mình bằng `which python` và kiểm tra bản Python dùng trong + PySINGA qua `otool -L `. Để sửa lỗi này, bạn compile + SINGA với đúng phiên bản mà SINGA cần. Cụ thể, nếu bạn tạo PySINGA từ nguồn, + bạn cần cụ thể đường dẫn khi gọi + [cmake](http://stackoverflow.com/questions/15291500/i-have-2-versions-of-python-installed-but-cmake-is-using-older-version-how-do) + + $ cmake -DPYTHON_LIBRARY=`python-config --prefix`/lib/libpython2.7.dylib -DPYTHON_INCLUDE_DIR=`python-config --prefix`/include/python2.7/ .. + + Nếu cài đặt PySINGA từ gói binary packages, vd. debian hay wheel, thì bạn cần + thay đổi trình thông dịch của python (python interpreter), vd., reset \$PATH + để đường dẫn dúng của Python ở đằng trước. diff --git a/docs-site/website/versioned_docs/version-4.3.0_Viet/contribute-code.md b/docs-site/website/versioned_docs/version-4.3.0_Viet/contribute-code.md new file mode 100644 index 0000000..c3b1d4c --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0_Viet/contribute-code.md @@ -0,0 +1,128 @@ +--- +id: version-4.2.0_Viet-contribute-code +title: Tham gia viết code +original_id: contribute-code +--- + + + +## Định dạng mã code + +Nền tảng code của SINGA tuân theo định dạng Google cho cả code +[CPP](http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml) và +[Python](http://google.github.io/styleguide/pyguide.html). + +Một cách đơn giản để thực hiện định dạng lập trình Google là sử dụng linting và +các công cụ định dạng trong Visual Studio Code editor: + +- [C/C++ extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools) +- [Python extension](https://marketplace.visualstudio.com/items?itemName=ms-python.python) +- [cpplint extension](https://marketplace.visualstudio.com/items?itemName=mine.cpplint) +- [Clang-Format](https://marketplace.visualstudio.com/items?itemName=xaver.clang-format) + +Sau khi cài extensions, chỉnh sửa tệp tin `settings.json`. + +```json +{ + "[cpp]": { + "editor.defaultFormatter": "xaver.clang-format" + }, + "cpplint.cpplintPath": "path/to/cpplint", + + "editor.formatOnSave": true, + "python.formatting.provider": "yapf", + "python.linting.enabled": true, + "python.linting.lintOnSave": true, + "clang-format.language.cpp.style": "google", + "python.formatting.yapfArgs": ["--style", "{based_on_style: google}"] +} +``` + +Dựa vào nền tảng bạn đang sử dụng. tệp tin user settings được đặt tại đây: + +1. Windows %APPDATA%\Code\User\settings.json +2. macOS "\$HOME/Library/Application Support/Code/User/settings.json" +3. Linux "\$HOME/.config/Code/User/settings.json" + +Thông số cấu hình cụ thể có trong các tệp tin config file tuơng ứng. Những công +cụ này sẽ tự động tìm kiếm các tập tin cấu hình configuration files trong root +của dự án, vd. `.pylintrc`. + +#### Cài đặt công cụ + +Tốt nhất là tất cả người tham gia viết mã code sử dụng cùng một phiên bản công +cụ định dạng mã code (clang-format 9.0.0 và yapf 0.29.0), để tất cả định dạng mã +code sẽ giống nhau dù thuộc về các PRs khác nhau, nhằm tránh tạo conflict trong +github pull request. + +Trước tiên, cài đặt LLVM 9.0 cung cấp clang-format phiên bản 9.0.0. Trang tải +LLVM là: + +- [LLVM](http://releases.llvm.org/download.html#9.0.0) + + - Trên Ubuntu + + ```sh + sudo apt-get install clang-format-9 + ``` + + - Trên Windows. Tải gói pre-built và cài đặt + +Sau đó, cài cpplint, pylint và yapf + +- Ubuntu or OSX: + + ``` + $ sudo pip install cpplint + $ which cpplint + /path/to/cpplint + + $ pip install yapf==0.29.0 + $ pip install pylint + ``` + +- Windows: Cài Anaconda cho gói quản lý package management. + + ``` + $ pip install cpplint + $ where cpplint + C:/path/to/cpplint.exe + + $ pip install yapf==0.29.0 + $ pip install pylint + ``` + +#### Sử dụng + +- Sau khi kích hoạt, linting sẽ tự động được áp dụng khi bạn chỉnh sửa các tập + tin mã code nguồn (source code file). Lỗi và cảnh báo sẽ hiển thị trên thanh + Visual Studio Code `PROBLEMS`. +- Định dạng mã code có thể thực hiện bằng cách sử dụng Command + Palette(`Shift+Ctrl+P` cho Windows hay `Shift+Command+P` cho OSX) và gõ + `Format Document`. + +#### Gửi + +Bạn cần phải chữa lỗi định dạng nếu có trước khi gửi đi pull requests. + +## Tạo Environment + +Chúng tôi khuyến khích dùng Visual Studio Code để viết code. Có thể cài các +Extensions như Python, C/C++, Code Spell Checker, autoDocstring, vim, Remote +Development. Tham khảo cấu hình (vd., `settings.json`) của các extensions +[tại đây](https://gist.github.com/nudles/3d23cfb6ffb30ca7636c45fe60278c55). + +Nếu bạn cập nhật mã code CPP, bạn cần recompile SINGA [từ nguồn](./build.md). +Nên sử dụng các công cụ cài đặt cơ bản trong `*-devel` Docker images hay +`conda build`. + +Nếu bạn chỉ cập nhật mã code Python, bạn cần cài đặt SINGAS một lần, sau đó copy +các tập tin Python cập nhật để thay thế chúng trong thư mục cài đặt Python, + +```shell +cp python/singa/xx.py /lib/python3.7/site-packages/singa/ +``` + +## Trình Tự + +Vui lòng tham khảo mục [git workflow](./git-workflow.md). diff --git a/docs-site/website/versioned_docs/version-4.3.0_Viet/contribute-docs.md b/docs-site/website/versioned_docs/version-4.3.0_Viet/contribute-docs.md new file mode 100644 index 0000000..cd6054b --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0_Viet/contribute-docs.md @@ -0,0 +1,106 @@ +--- +id: version-4.2.0_Viet-contribute-docs +title: Tham gia chỉnh sửa Hướng Dẫn Sử Dụng +original_id: contribute-docs +--- + + + +Hướng Dẫn Sử Dụng có hai dạng, dạng tập tin markdown và dạng sử dụng API +reference. Tài liệu này giới thiệu vài công cụ và chỉ dẫn trong việc chuẩn bị +các tập tin nguồn markdown và chú thích API. + +Tập tin markdown sẽ được sử dụng trong việc tạo trang HTML qua +[Docusaurus](https://docusaurus.io/); Chú thích API (từ nguồn mã code) sẽ được +sử dụng để tạo các trang tham khảo API sử dụng Sphinx (cho Python) và Doxygen +(cho CPP). + +## Tập Tin Markdown + +Làm theo [định dạng Văn bản Google](https://developers.google.com/style). Ví dụ, + +1. Bỏ 'vui lòng' (please) khỏi bất cứ hướng dẫn sử dụng nào. 'Please click...' + thành 'Click ...'. +2. Làm theo + [qui tắc viết hoa tiêu chuẩn](https://owl.purdue.edu/owl/general_writing/mechanics/help_with_capitals.html). +3. Sử dụng 'bạn' thay cho 'chúng tôi' trong hướng dẫn. +4. Sử dụng thì hiện tại và tránh sử dụng từ 'sẽ' +5. Nên dùng dạng chủ động thay vì bị động + +Thêm vào đó, để cho nội dung hướng dẫn sủ dụng thống nhất, + +1. Viết câu ngắn, chẳng hạn độ dài <=80 +2. Sử dụng đường dẫn liên quan, mặc định chúng ta đang ở thư mục root của repo, + vd., `doc-site/docs` để chỉ `singa-doc/docs-site/docs` +3. Nhấn mạnh câu lệnh, đường dẫn, class, function và biến sử dụng backticks, + vd., `Tensor`, `singa-doc/docs-site/docs`. +4. Để nêu bật các điều khoản/khái niệm, sử dụng _graph_ hoặc **graph** + +[Cộng cụ prettier](https://prettier.io/) được sử dụng bởi dự án này sẽ tự làm +định dạng code dựa trên +[cấu hình](https://github.com/apache/singa-doc/blob/master/docs-site/.prettierrc) +khi thực hiện `git commit`. Ví dụ, nó sẽ gói chữ trong các tập tin markdown +thành nhiều nhất 80 kí tự (trừ các dòng chú thích). + +Khi giới thiệu một khái niệm (concept) (vd., class `Tensor`), đưa ra khái quát +chung (mục đích và mối quan hệ với các khái niệm khác), APIs và ví dụ. Google +colab có thể được sử dụng để mô phỏng điều này. + +Tham khảo [trang](https://github.com/apache/singa-doc/tree/master/docs-site) để +biết thêm chi tiết về cách chỉnh sửa các tập tin markdown và xây dựng website. + +## Tham Khảo API + +### CPP API + +Thực hiện theo +[Mẫu chú thích của Google CPP](https://google.github.io/styleguide/cppguide.html#Comments). + +Để tạo văn bản, chạy "doxygen" từ thư mục doc (khuyến khích Doxygen >= 1.8) + +### Python API + +Thực hiện theo +[Mẫu Google Python DocString](http://google.github.io/styleguide/pyguide.html#38-comments-and-docstrings). + +## Visual Studio Code (vscode) + +Nếu bạn sử dụng vscode để viết code, các plugins sau sẽ giúp ích. + +### Docstring Snippet + +[autoDocstring](https://marketplace.visualstudio.com/items?itemName=njpwerner.autodocstring) +tạo docstring của functions, classes, v.v. Lựa chọn định dạng DocString to +`google`. + +### Kiểm Tra Lỗi Chính Tả + +[Code Spell Checker](https://marketplace.visualstudio.com/items?itemName=streetsidesoftware.code-spell-checker) +có thể được cơ cấu để kiểm tra chú thích trong mã code, hoặc các tập tin .md và +.rst. + +Để kiểm tra lỗi chính tả cho các dòng chú thích trong code Python, thêm vào các +snippet sau qua `File - Preferences - User Snippets - python.json` + + "cspell check" : { + "prefix": "cspell", + "body": [ + "# Chỉ dẫn kiểm tra lỗi chính tả cho các dòng chú thích trong code python và c/cpp", + "# cSpell:includeRegExp #.* ", + "# cSpell:includeRegExp (\"\"\"|''')[^\1]*\1", + "# cSpell: CStyleComment", + ], + "description": "# chỉ kiểm tra lỗi chính tả cho chú thích trong python" + } + +Để kiểm tra lỗi chính tả cho các dòng chú thích trong code Cpp, thêm vào các +snippet sau qua `File - Preferences - User Snippets - cpp.json` + + "cspell check" : { + "prefix": "cspell", + "body": [ + "// Chỉ dẫn kiểm tra lỗi chính tả cho các dòng chú thích trong code cpp", + "// cSpell:includeRegExp CStyleComment", + ], + "description": "# chỉ kiểm tra lỗi chính tả cho chú thích trong cpp" + } diff --git a/docs-site/website/versioned_docs/version-4.3.0_Viet/device.md b/docs-site/website/versioned_docs/version-4.3.0_Viet/device.md new file mode 100644 index 0000000..f621333 --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0_Viet/device.md @@ -0,0 +1,33 @@ +--- +id: version-4.2.0_Viet-device +title: Device +original_id: device +--- + + + +Device dùng ở đây nghĩa là thiết bị phần cứng với bộ nhớ và các bộ phận máy +tính. Tất cả [Tensor operations](./tensor) được sắp xếp bởi các thiết bị +resident device khi chạy. Bộ nhớ của Tensor luôn luôn được quản lý bởi memory +manager của thiết bị đó. Bởi vậy việc tận dụng tối đa bộ nhớ và thực hiện được +tiến hành tại Device class. + +## Các thiết bị cụ thể + +Hiện tại, SINGA được chạy trên ba Device, + +1. CudaGPU cho cạc Nvidia GPU card chạy code Cuda +2. CppCPU cho CPU chạy Cpp code +3. OpenclGPU cho cạc GPU chạy OpenCL code + +## Ví Dụ Sử Dụng + +Code dưới đây là ví dụ về việc tạo device: + +```python +from singa import device +cuda = device.create_cuda_gpu_on(0) # sử dụng cạc GPU với ID 0 +host = device.get_default_device() # tạo host mặc định cho device (CppCPU) +ary1 = device.create_cuda_gpus(2) # tạo 2 devices, bắt đầu từ ID 0 +ary2 = device.create_cuda_gpus([0,2]) # tạo 2 devices với ID 0 và 2 +``` diff --git a/docs-site/website/versioned_docs/version-4.3.0_Viet/dist-train.md b/docs-site/website/versioned_docs/version-4.3.0_Viet/dist-train.md new file mode 100644 index 0000000..54998bd --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0_Viet/dist-train.md @@ -0,0 +1,447 @@ +--- +id: version-4.2.0_Viet-dist-train +title: Distributed Training +original_id: dist-train +--- + + + +SINGA hỗ trợ data parallel training trên nhiều GPUs (trên một node hoặc nhiều +node khác nhau). Sơ đồ sau mô phỏng data parallel training: + +![MPI.png](assets/MPI.png) + +Trong distributed training, mỗi chỉ lệnh (gọi là worker) chạy một training +script trên một máy GPU. Mỗi chỉ lệnh (process) có một communication rank riêng. +Dữ liệu để training được phân cho các worker và model thì được sao chép cho mỗi +worker. Ở mỗi vòng, worker đọc một mini-batch dữ liệu (vd., 256 hình ảnh) từ +phần được chia và chạy thuật toán BackPropagation để tính ra độ dốc (gradient) +của weight, được lấy trung bình qua all-reduce (cung cấp bởi +[NCCL](https://developer.nvidia.com/nccl)) để cập nhật weight theo thuật toán +stochastic gradient descent (SGD). + +Hàm all-reduce operation bởi NCCL có thể được sử dụng để giảm và đồng bộ hoá độ +dốc từ các máy GPU các nhau. Xem thử training với 4 GPUs như dưới đây. Sau khi +độ dốc (gradients) từ 4 GPUs được tính, all-reduce sẽ trả lại tổng độ dốc +(gradient) cho các GPU và đưa tới mỗi GPU. Sau đó có thể dễ dàng tính ra độ dốc +trung bình. + +![AllReduce.png](assets/AllReduce.png) + +## Sử Dụng + +SINGA áp dụng một module gọi là `DistOpt` (là dạng con của `Opt`) cho +distributed training. Nó gói lại normal SGD optimizer và gọi `Communicator` để +động bộ hoá độ dốc. Ví dụ sau mô phỏng cách sử dụng `DistOpt` để training một +CNN model với dữ liệu MNIST. Nguồn code có thể tìm +[tại đây](https://github.com/apache/singa/blob/master/examples/cnn/), và +[Colab notebook](). + +### Code Ví Dụ + +1. Định nghĩa neural network model: + +```python +class CNN(model.Model): + + def __init__(self, num_classes=10, num_channels=1): + super(CNN, self).__init__() + self.conv1 = layer.Conv2d(num_channels, 20, 5, padding=0, activation="RELU") + self.conv2 = layer.Conv2d(20, 50, 5, padding=0, activation="RELU") + self.linear1 = layer.Linear(500) + self.linear2 = layer.Linear(num_classes) + self.pooling1 = layer.MaxPool2d(2, 2, padding=0) + self.pooling2 = layer.MaxPool2d(2, 2, padding=0) + self.relu = layer.ReLU() + self.flatten = layer.Flatten() + self.softmax_cross_entropy = layer.SoftMaxCrossEntropy() + + def forward(self, x): + y = self.conv1(x) + y = self.pooling1(y) + y = self.conv2(y) + y = self.pooling2(y) + y = self.flatten(y) + y = self.linear1(y) + y = self.relu(y) + y = self.linear2(y) + return y + + def train_one_batch(self, x, y, dist_option='fp32', spars=0): + out = self.forward(x) + loss = self.softmax_cross_entropy(out, y) + + # cho phép nhiều lựa chọn dùng trong distributed training + # Tham khảo mục "Optimizations về Distributed Training" + if dist_option == 'fp32': + self.optimizer(loss) + elif dist_option == 'fp16': + 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 + +# tạo model +model = CNN() +``` + +2. Tạo `DistOpt` instance và đính nó vào model đã tạo: + +```python +sgd = opt.SGD(lr=0.005, momentum=0.9, weight_decay=1e-5) +sgd = opt.DistOpt(sgd) +model.set_optimizer(sgd) +dev = device.create_cuda_gpu_on(sgd.local_rank) +``` + +Đây là giải thích cho các biến sử dụng trong code: + +(i) `dev` + +dev dùng để chỉ `Device` instance, nơi tải dữ liệu và chạy CNN model. + +(ii)`local_rank` + +Local rank chỉ số GPU mà chỉ lệnh (process) hiện tại đang sử dụng trên cùng một +node. Ví dụ, nếu bạn đang sử dụng một node có 2 GPUs, `local_rank=0` nghĩa là +chỉ lệnh này đang sử dụng máy GPU đầu tiên, trong khi `local_rank=1` nghĩa là +đang sử dụng máy GPU thứ hai. Sử dụng MPI hay đa xử lý, bạn có thể chạy cùng một +tập lệnh training chỉ khác giá trị của `local_rank`. + +(iii)`global_rank` + +Rank trong global biểu thị global rank cho tất cả các chỉ lệnh (process) trong +các nodes mà bạn đang sử dụng. Lấy ví dụ trường hợp bạn có 3 nodes và mỗi một +node có hai GPUs, `global_rank=0` nghĩa là chỉ lệnh đang sử dụng máy GPU đầu +tiên ở node đầu tiên, `global_rank=2` nghĩa là chỉ lệnh đang sử dụng máy GPU đầu +tiên ở node thứ 2, và `global_rank=4` nghĩa là chỉ lệnh đang sử dụng máy GPU đầu +tiên ở node thứ 3. + +3. Tải và phân chia dữ liệu để training/validation + +```python +def data_partition(dataset_x, dataset_y, global_rank, world_size): + data_per_rank = dataset_x.shape[0] // world_size + idx_start = global_rank * data_per_rank + idx_end = (global_rank + 1) * data_per_rank + return dataset_x[idx_start:idx_end], dataset_y[idx_start:idx_end] + +train_x, train_y, test_x, test_y = load_dataset() +train_x, train_y = data_partition(train_x, train_y, + sgd.global_rank, sgd.world_size) +test_x, test_y = data_partition(test_x, test_y, + sgd.global_rank, sgd.world_size) +``` + +Một phần của bộ dữ liệu (dataset) được trả lại cho `dev`. + +Tại đây, `world_size` thể hiện tổng số chỉ lệnh trong tất cả các node mà bạn +đang sử dụng cho distributed training. + +4. Khởi tạo và đồng bộ các tham số của model cho tất cả workers: + +```python +# Đồng bộ tham số ban đầu +tx = tensor.Tensor((batch_size, 1, IMG_SIZE, IMG_SIZE), dev, tensor.float32) +ty = tensor.Tensor((batch_size, num_classes), dev, tensor.int32) +model.compile([tx], is_train=True, use_graph=graph, sequential=True) +... +# Sử dụng cùng một random seed cho các ranks khác nhau +seed = 0 +dev.SetRandSeed(seed) +np.random.seed(seed) +``` + +5. Chạy BackPropagation và distributed SGD + +```python +for epoch in range(max_epoch): + for b in range(num_train_batch): + x = train_x[idx[b * batch_size: (b + 1) * batch_size]] + y = train_y[idx[b * batch_size: (b + 1) * batch_size]] + tx.copy_from_numpy(x) + ty.copy_from_numpy(y) + # Train the model + out, loss = model(tx, ty) +``` + +### Hướng Dẫn Thực Hiện + +Có hai cách để bắt đầu quá trình training: MPI hoặc Python đa xử lý. + +#### Python Đa Xử Lý + +Chạy trên một node với nhiều GPUs, trong đó mỗi GPU là một worker. + +1. Đặt tất cả các training codes trong cùng một hàm (function) + +```python +def train_mnist_cnn(nccl_id=None, local_rank=None, world_size=None): + ... +``` + +2. Tạo `mnist_multiprocess.py` + +```python +if __name__ == '__main__': + # Generate a NCCL ID to be used for collective communication + nccl_id = singa.NcclIdHolder() + + # Define the number of GPUs to be used in the training process + world_size = int(sys.argv[1]) + + # Define and launch the multi-processing + import multiprocessing + process = [] + for local_rank in range(0, world_size): + process.append(multiprocessing.Process(target=train_mnist_cnn, + args=(nccl_id, local_rank, world_size))) + + for p in process: + p.start() +``` + +Dưới đây là giải thích cho các biến tạo ở trên: + +(i) `nccl_id` + +Lưu ý rằng chúng ta cần phải tạo một NCCL ID ở đây để sử dụng cho collective +communication, sau đó gửi nó tới tất cả các chỉ lệnh. NCCL ID giống như là vé +vào cửa, khi chỉ có chỉ lệnh với ID này có thể tham gia vào quá trình +all-reduce. (Về sua nếu dùng MPI, thì việc sử dụng NCCL ID là không cần thiết, +bởi vì ID được gửi đi bởi MPI trong code của chúng tôi một cách tự động) + +(ii) `world_size` + +world_size là số lượng máy GPUs bạn muốn sử dụng cho training. + +(iii) `local_rank` + +local_rank xác định local rank của distributed training và máy gpu được sử dụng +trong chỉ lệnh. Trong code bên trên, for loop được sử dụng để chạy hàm train +function, và local_rank chạy vòng từ 0 tới world_size. Trong trường hợp này, chỉ +lệnh khác nhau có thể sử dụng máy GPUs khác nhau để training. + +Tham số để tạo `DistOpt` instance cần được cập nhật như sau: + +```python +sgd = opt.DistOpt(sgd, nccl_id=nccl_id, local_rank=local_rank, world_size=world_size) +``` + +3. Chạy `mnist_multiprocess.py` + +```sh +python mnist_multiprocess.py 2 +``` + +Kết qủa hiển thị tốc độ so với training trên một máy GPU. + +``` +Starting Epoch 0: +Training loss = 408.909790, training accuracy = 0.880475 +Evaluation accuracy = 0.956430 +Starting Epoch 1: +Training loss = 102.396790, training accuracy = 0.967415 +Evaluation accuracy = 0.977564 +Starting Epoch 2: +Training loss = 69.217010, training accuracy = 0.977915 +Evaluation accuracy = 0.981370 +Starting Epoch 3: +Training loss = 54.248390, training accuracy = 0.982823 +Evaluation accuracy = 0.984075 +Starting Epoch 4: +Training loss = 45.213406, training accuracy = 0.985560 +Evaluation accuracy = 0.985276 +Starting Epoch 5: +Training loss = 38.868435, training accuracy = 0.987764 +Evaluation accuracy = 0.986278 +Starting Epoch 6: +Training loss = 34.078186, training accuracy = 0.989149 +Evaluation accuracy = 0.987881 +Starting Epoch 7: +Training loss = 30.138697, training accuracy = 0.990451 +Evaluation accuracy = 0.988181 +Starting Epoch 8: +Training loss = 26.854443, training accuracy = 0.991520 +Evaluation accuracy = 0.988682 +Starting Epoch 9: +Training loss = 24.039650, training accuracy = 0.992405 +Evaluation accuracy = 0.989083 +``` + +#### MPI + +Có thể dùng cho cả một node và nhiều node miễn là có nhiều máy GPUs. + +1. Tạo `mnist_dist.py` + +```python +if __name__ == '__main__': + train_mnist_cnn() +``` + +2. Tạo một hostfile cho MPI, vd. hostfile dưới đây sử dụng 2 chỉ lệnh (vd., 2 + GPUs) trên một node + +```txt +localhost:2 +``` + +3. Khởi động quá trình training qua `mpiexec` + +```sh +mpiexec --hostfile host_file python mnist_dist.py +``` + +Kết qủa có thể hiển thị tốc độ so với training trên một máy GPU. + +``` +Starting Epoch 0: +Training loss = 383.969543, training accuracy = 0.886402 +Evaluation accuracy = 0.954327 +Starting Epoch 1: +Training loss = 97.531479, training accuracy = 0.969451 +Evaluation accuracy = 0.977163 +Starting Epoch 2: +Training loss = 67.166870, training accuracy = 0.978516 +Evaluation accuracy = 0.980769 +Starting Epoch 3: +Training loss = 53.369656, training accuracy = 0.983040 +Evaluation accuracy = 0.983974 +Starting Epoch 4: +Training loss = 45.100403, training accuracy = 0.985777 +Evaluation accuracy = 0.986078 +Starting Epoch 5: +Training loss = 39.330826, training accuracy = 0.987447 +Evaluation accuracy = 0.987179 +Starting Epoch 6: +Training loss = 34.655270, training accuracy = 0.988799 +Evaluation accuracy = 0.987780 +Starting Epoch 7: +Training loss = 30.749735, training accuracy = 0.989984 +Evaluation accuracy = 0.988281 +Starting Epoch 8: +Training loss = 27.422146, training accuracy = 0.991319 +Evaluation accuracy = 0.988582 +Starting Epoch 9: +Training loss = 24.548153, training accuracy = 0.992171 +Evaluation accuracy = 0.988682 +``` + +## Tối Ưu Hoá Distributed Training + +SINGA cung cấp chiến lược đa tối ưu hoá cho distributed training để giảm +communication cost. Tham khảo API của `DistOpt` cho cấu hình của mỗi cách. + +Khi sử dụng `model.Model` để tạo một model, cần phải đặt các lựa chọn cho +distributed training trong phương pháp `train_one_batch`. Tham khảo code ví dụ +trên đầu trang. Bạn có thể chỉ cần copy code cho các lựa chọn và sử dụng nó cho +các model khác. Với các lựa chọn xác định, ta có thể đặt tham số`dist_option` và +`spars` khi bắt đầu training với `model(tx, ty, dist_option, spars)` + +### Không Tối Ưu Hoá + +```python +out, loss = model(tx, ty) +``` + +`loss` là output tensor từ hàm loss function, vd., cross-entropy cho +classification tasks. + +### Half-precision Gradients + +```python +out, loss = model(tx, ty, dist_option = 'fp16') +``` + +Chuyển đổi gía trị độ dốc sang hiển thị dạng 16-bit (vd., half-precision) trước +khi gọi hàm all-reduce. + +### Đồng Bộ Cục Bộ (Partial Synchronization) + +```python +out, loss = model(tx, ty, dist_option = 'partialUpdate') +``` + +Ở mỗi vòng lặp (iteration), mỗi rank thực hiện việc cập nhật sgd. Sau đó chỉ một +nhóm tham số là được tính trung bình để đồng bộ hoá. Điều này giúp tiết kiệm +communication cost. Độ lớn của nhóm này được xác định khi tạo hàm `DistOpt` +instance. + +### Phân Bổ Độ Dốc (Gradient Sparsification) + +Kế hoạch phân bổ để chọn ra một nhóm nhỏ độ dốc nhằm thực hiện all-reduce. Có +hai cách: + +- Chọn K phần tử lớn nhất. spars là một phần (0 - 1) của tổng số phần tử được + chọn. + +```python +out, loss = model(tx, ty, dist_option = 'sparseTopK', spars = spars) +``` + +- Tất cả độ dốc có giá trị tuyệt đối lớn hơn ngưỡng spars đặt trước được lựa + chọn. + +```python +out, loss = model(tx, ty, dist_option = 'sparseThreshold', spars = spars) +``` + +Các hyper-parameter được cấu tạo khi tạo hàm `DistOpt` instance. + +## Thực Hiện + +Mục này chủ yếu dành cho các lập trình viên (developer) muốn biết lập trình +trong distribute module được thực hiện như thế nào. + +### Giao Diện C cho Bộ Chuyển Mạch (communicator) NCCL + +Trước tiên, communication layer được lập trình bằng ngôn ngữ C +[communicator.cc](https://github.com/apache/singa/blob/master/src/io/communicator.cc). +Nó áp dụng NCCL library cho collective communication. + +Có hai hàm tạo nên communicator, một cho MPI và một cho đa phương thức +(multiprocess). + +(i) Hàm tạo sử dụng MPI + +Hàm tạo bước đầu sẽ sử dụng global rank và world size, sau đó tính toán ra local +rank. Tiếp theo, rank 0 sẽ tạo ra NCCL ID và phát nó lên mỗi rank. Sau đó, nó +gọi hàm setup để khởi tạo NCCL communicator, cuda streams, và buffers. + +(ii) Hàm tạo sử dụng Python đa phương thức + +Hàm tạo bước đầu sẽ sử dụng rank, world size, và NCCL ID từ input argument. Sau +đó, nó gọi hàm setup function để khởi tạo NCCL communicator, cuda streams, và +buffers. + +Sau khi khởi động, nó thực hiện chức năng all-reduce để đồng bộ hoá các tham số +model và độ dốc. Ví dụ, synch sử dụng một input tensor và tiến hành all-reduce +qua đoạn chương trình NCCL. Sau khi gọi synch, cần gọi hàm wait để đợi hàm +all-reduce operation kết thúc. + +### Giao Diện Python của DistOpt + +Sau đó, giao diện python sẽ tạo ra một +[DistOpt](https://github.com/apache/singa/blob/master/python/singa/opt.py) class +để gói một +[optimizer](https://github.com/apache/singa/blob/master/python/singa/opt.py) +object để thực hiện distributed training dựa trên MPI hoặc đa xử lý. Trong khi +khởi động, nó tạo ra một NCCL communicator object (từ giao diện C đề cập ở mục +nhỏ phía trên). Sau đó, communicator object này được sử dụng trong mỗi hàm +all-reduce trong DistOpt. + +Trong MPI hoặc đa xử lý, mỗi chỉ lệnh có một rank, cho biết thông tin máy GPU +nào qui trình này đang sử dụng. Dữ liệu training được chia nhỏ để mỗi chỉ lệnh +có thể đánh giá sub-gradient dựa trên dữ liệu đã chia trước đó. Sau khi +sub-gradient được tạo ra ở mỗi chỉ lệnh, độ dốc stochastic gradient tổng hợp sẽ +được tạo ra bằng cách all-reduce các sub-gradients đánh giá bởi tất cả các chỉ +lệnh. diff --git a/docs-site/website/versioned_docs/version-4.3.0_Viet/download.md b/docs-site/website/versioned_docs/version-4.3.0_Viet/download.md new file mode 100644 index 0000000..315a0b2 --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0_Viet/download.md @@ -0,0 +1,206 @@ +--- +id: version-4.2.0_Viet-downloads +title: Tải SINGA +original_id: downloads +--- + + + +## Kiểm Chứng + +Để kiểm chứng tập tin tar.gz đã tải, tải +[KEYS](https://www.apache.org/dist/singa/KEYS) và tập tin ASC sau đó thực hiện +các lệnh sau + +```shell +% gpg --import KEYS +% gpg --verify downloaded_file.asc downloaded_file +``` + +Bạn có thể kiểm tra giá trị của SHA512 hoặc MD5 để xem liệu việc tải về đã hoàn +thành chưa. + +## V3.1.0 (30 tháng 10 năm 2020): + +- [Apache SINGA 3.1.0](http://www.apache.org/dyn/closer.cgi/singa/3.1.0/apache-singa-3.1.0.tar.gz) + [\[SHA512\]](https://www.apache.org/dist/singa/3.1.0/apache-singa-3.1.0.tar.gz.sha512) + [\[ASC\]](https://www.apache.org/dist/singa/3.1.0/apache-singa-3.1.0.tar.gz.asc) +- [Release Notes 3.1.0](http://singa.apache.org/docs/releases/RELEASE_NOTES_3.1.0) +- Thay đổi chung: + - Cập nhật Tensor core: + - Hỗ trợ tensor transformation (reshape, transpose) cho tensors có tới 6 + chiều (dimensions). + - Áp dụn traverse_unary_transform ở Cuda backend, tương tự như CPP backend + one. + - Thêm hàm tensor operators vào autograd module. + - Cải tạo lại sonnx để + - Hỗ trợ việc tạo hàm operators từ cả layer và autograd. + - Viết lại SingaRep để SINGA representation mạnh và nhanh hơn. + - Thêm SONNXModel áp dụng từ Model để API và các tính năng đồng bộ với nhau. + * Thay thế Travis CI với trình tự Github. Thêm quản lý chất lượng và độ bao + phủ. + * Thêm Tập lệnh compiling và packaging nhằm tạo gói wheel packages cho + distribution. + * Fix bugs + - Hoàn thiện Tập lệnh training cho ví dụ về IMDB LSTM model. + - Ổn định lại hàm Tensor operation Mult khi sử dụng Broadcasting. + - Hàm Gaussian trong Tensor giờ có thể chạy trên Tensor với kích thước lẻ. + - Cập nhật hàm hỗ trợ chạy thử gradients() trong autograd để tìm tham số + gradient qua tham số python object id khi chạy thử. + +## V3.0.0 (18 April 2020): + +- [Apache SINGA 3.0.0](https://archive.apache.org/dist/singa/3.0.0/apache-singa-3.0.0.tar.gz) + [\[SHA512\]](https://archive.apache.org/dist/singa/3.0.0/apache-singa-3.0.0.tar.gz.sha512) + [\[ASC\]](https://archive.apache.org/dist/singa/3.0.0/apache-singa-3.0.0.tar.gz.asc) +- [Ghi Chú Phát Hành 3.0.0](http://singa.apache.org/docs/releases/RELEASE_NOTES_3.0.0) +- Các tính năng mới và thay đổi chính, + - Nâng cấp ONNX. Thử nghiệm nhiều ONNX models trên SINGA. + - Thực hiện Distributed training với MPI và tối ưu hoá NCCL Communication qua + phân bổ và nén độ dốc, và truyền tải phân khúc. + - Xây dựng và tối ưu hoá tốc độ và bộ nhớ sử dụng graph của Computational + graph. + - Lập trang Tài Liệu sử dụng mới (singa.apache.org) và website tham khảo API + (apache-singa.rtfd.io). + - CI cho việc kiểm tra chất lượng mã code. + - Thay thế MKLDNN bằng DNNL + - Cập nhật APIs cho tensor để hỗ trợ hàm broadcasting. + - Tạo autograd operators mới để hỗ trợ các ONNX models. + +## Incubating v2.0.0 (20 tháng 4 năm 2019): + +- [Apache SINGA 2.0.0 (incubating)](https://archive.apache.org/dist/incubator/singa/2.0.0/apache-singa-incubating-2.0.0.tar.gz) + [\[SHA512\]](https://archive.apache.org/dist/incubator/singa/2.0.0/apache-singa-incubating-2.0.0.tar.gz.sha512) + [\[ASC\]](https://archive.apache.org/dist/incubator/singa/2.0.0/apache-singa-incubating-2.0.0.tar.gz.asc) +- [Ghi Chú Phát Hành 2.0.0 (incubating)](http://singa.apache.org/docs/releases/RELEASE_NOTES_2.0.0.html) +- Các tính năng mới và thay đổi chính, + - Nâng cấp autograd (cho Convolution networks và recurrent networks) + - Hỗ trợ ONNX + - Cải thiện hàm CPP operations qua Intel MKL DNN lib + - Thực hiện tensor broadcasting + - Chuyển Docker images dưới tên sử dụng trong Apache + - Cập nhật các phiên bản dependent lib trong conda-build config + +## Incubating v1.2.0 (6 June 2018): + +- [Apache SINGA 1.2.0 (incubating)](https://archive.apache.org/dist/incubator/singa/1.2.0/apache-singa-incubating-1.2.0.tar.gz) + [\[SHA512\]](https://archive.apache.org/dist/incubator/singa/1.2.0/apache-singa-incubating-1.2.0.tar.gz.sha512) + [\[ASC\]](https://archive.apache.org/dist/incubator/singa/1.2.0/apache-singa-incubating-1.2.0.tar.gz.asc) +- [Release Notes 1.2.0 (incubating)](http://singa.apache.org/docs/releases/RELEASE_NOTES_1.2.0.html) +- Các tính năng mới và thay đổi chính, + - Thực hiện autograd (đang hỗ trợ MLP model) + - Nâng cấp PySinga để hỗ trợ Python 3 + - Cải thiện Tensor class với mục stride + - Nâng cấp cuDNN từ V5 sang V7 + - Thêm VGG, Inception V4, ResNet, và DenseNet cho ImageNet classification + - Tạo alias cho gói conda packages + - Hoàn thiện Tài liệu sử dụng bằng tiếng Trung + - Thêm hướng dẫn chạy Singa trên Windows + - Cập nhật compilation, CI + - Sửa lỗi nếu có + +## Incubating v1.1.0 (12 February 2017): + +- [Apache SINGA 1.1.0 (incubating)](https://archive.apache.org/dist/incubator/singa/1.1.0/apache-singa-incubating-1.1.0.tar.gz) + [\[MD5\]](https://archive.apache.org/dist/incubator/singa/1.1.0/apache-singa-incubating-1.1.0.tar.gz.md5) + [\[ASC\]](https://archive.apache.org/dist/incubator/singa/1.1.0/apache-singa-incubating-1.1.0.tar.gz.asc) +- [Release Notes 1.1.0 (incubating)](http://singa.apache.org/docs/releases/RELEASE_NOTES_1.1.0.html) +- Các tính năng mới và thay đổi chính, + - Tạo Docker images (phiên bản CPU và GPU) + - Tạo Amazon AMI cho SINGA (phiên bản CPU) + - Tích hợp với Jenkins để tự động tạo gói Wheel và Debian (cho cài đặt), và + cập nhật website. + - Nâng cấp FeedFowardNet, vd., nhiều mode cho inputs và verbose để sửa lỗi + - Thêm Concat và Slice layers + - Mở rộng CrossEntropyLoss nhằm chấp nhật instance với nhiều labels + - Thêm image_tool.py với phương thức image augmentation + - Hỗ trợ tải và lưu model qua Snapshot API + - Compile SINGA source trên Windows + - Compile những dependent libraries bắt buộc cùng với SINGA code + - Kích hoạt Java binding (cơ bản) cho SINGA + - Thêm phiên bản ID trong kiểm soát tập tin + - Thêm gói sử dụng Rafiki cung cấp RESTFul APIs + - Thêm ví dụ pretrained từ Caffe, bao gồm GoogleNet + +## Incubating v1.0.0 (8 September 2016): + +- [Apache SINGA 1.0.0 (incubating)](https://archive.apache.org/dist/incubator/singa/1.0.0/apache-singa-incubating-1.0.0.tar.gz) + [\[MD5\]](https://archive.apache.org/dist/incubator/singa/1.0.0/apache-singa-incubating-1.0.0.tar.gz.md5) + [\[ASC\]](https://archive.apache.org/dist/incubator/singa/1.0.0/apache-singa-incubating-1.0.0.tar.gz.asc) +- [Release Notes 1.0.0 (incubating)](http://singa.apache.org/docs/releases/RELEASE_NOTES_1.0.0.html) +- Các tính năng mới và thay đổi chính, + - Tạo Tensor nhằm hỗ trợ nhiều model khác nhau. + - Tạo Device để chạy trên các thiết bị phần cứng khác nhau, bao gồm CPU, + (Nvidia/AMD) GPU và FPGA (sẽ thử nghiệm ở các phiên bản mới). + - Thay thế GNU autotool với cmake khi compilation. + - Hỗ trợ Mac OS + - Cải thiện Python binding, bao gồm cài đặt và lập trình. + - Tạo thêm nhiều deep learning models, bao gồm VGG và ResNet + - Thêm IO classes để đọc/viết tập tin và mã hoá/giải mã dữ liệu + - Các thành phần network communication mới trực tiếp từ Socket. + - Cudnn V5 với Dropout và RNN layers. + - Thay thế công cụ xây dựng website từ maven sang Sphinx + - Tích hợp Travis-CI + +## Incubating v0.3.0 (20 April 2016): + +- [Apache SINGA 0.3.0 (incubating)](https://archive.apache.org/dist/incubator/singa/0.3.0/apache-singa-incubating-0.3.0.tar.gz) + [\[MD5\]](https://archive.apache.org/dist/incubator/singa/0.3.0/apache-singa-incubating-0.3.0.tar.gz.md5) + [\[ASC\]](https://archive.apache.org/dist/incubator/singa/0.3.0/apache-singa-incubating-0.3.0.tar.gz.asc) +- [Release Notes 0.3.0 (incubating)](http://singa.apache.org/docs/releases/RELEASE_NOTES_0.3.0.html) +- Các tính năng mới và thay đổi chính, + - Training trên nhóm máy GPU: cho phép training các deep learning models trên + một nhóm máy GPU + - Cải thiện Python wrapper khiến cho job configuration trở nên dễ dàng, bao + gồm neural net và thuật toán SGD. + - Thêm cập nhật SGD updaters mới, bao gồm Adam, AdaDelta và AdaMax. + - Cài đặt cần ít dependent libraries hơn cho mỗi node training. + - Đa dạng training với CPU và GPU. + - Hỗ trợ cuDNN V4. + - Tìm nạp trước dữ liệu. + - Sửa lỗi nếu có. + +## Incubating v0.2.0 (14 January 2016): + +- [Apache SINGA 0.2.0 (incubating)](https://archive.apache.org/dist/incubator/singa/0.2.0/apache-singa-incubating-0.2.0.tar.gz) + [\[MD5\]](https://archive.apache.org/dist/incubator/singa/0.2.0/apache-singa-incubating-0.2.0.tar.gz.md5) + [\[ASC\]](https://archive.apache.org/dist/incubator/singa/0.2.0/apache-singa-incubating-0.2.0.tar.gz.asc) +- [Release Notes 0.2.0 (incubating)](http://singa.apache.org/docs/releases/RELEASE_NOTES_0.2.0.html) +- Các tính năng mới và thay đổi chính, + - Training trên GPU cho phép training các models phức tạp trên một node với + nhiều card GPU. + - Chia nhỏ Hybrid neural net hỗ trợ dữ liệu và model song song cùng lúc. + - Cải thiện Python wrapper khiến cho job configuration trở nên dễ dàng, bao + gồm neural net và thuật toán SGD. + - Áp dụng RNN model và thuật toán BPTT để hỗ trợ các ứng dụng dựa trên RNN + models, e.g., GRU. + - Tích hợp các phần mêm đám mây bao gồm Mesos, Docker và HDFS. + - Cung cấp hình ảnh cấu trúc neural net và thông tin layer, hỗ trợ việc sửa + lỗi. + - Hàm Linear algebra và các hàm ngẫu nhiên không dùng Blobs và chỉ điểm dữ + liệu thô. + - Tạo layers mới, bao gồm SoftmaxLayer, ArgSortLayer, DummyLayer, RNN layers + và cuDNN layers. + - Cập nhật Layer class để chứa nhiều data/grad Blobs. + - Trích xuất các features và thử nghiệm hiệu quả cho dữ liệu mới bằng cách tải + các tham số model đã được train từ trước. + - Thêm Store class cho hàm IO operations. + +## Incubating v0.1.0 (8 October 2015): + +- [Apache SINGA 0.1.0 (incubating)](https://archive.apache.org/dist/incubator/singa/apache-singa-incubating-0.1.0.tar.gz) + [\[MD5\]](https://archive.apache.org/dist/incubator/singa/apache-singa-incubating-0.1.0.tar.gz.md5) + [\[ASC\]](https://archive.apache.org/dist/incubator/singa/apache-singa-incubating-0.1.0.tar.gz.asc) +- [Amazon EC2 image](https://console.aws.amazon.com/ec2/v2/home?region=ap-southeast-1#LaunchInstanceWizard:ami=ami-b41001e6) +- [Release Notes 0.1.0 (incubating)](http://singa.apache.org/docs/releases/RELEASE_NOTES_0.1.0.html) +- Các thay đổi chính gồm có, + - Cài đặt sử dụng tiện ích GNU build + - Tập lệnh cho job management với zookeeper + - Lập trình model dựa trên NeuralNet và trích xuất Layer. + - Kết cấu hệ thống dựa trên Worker, Server và Stub. + - Training models từ ba model khác nhau, là feed-forward models, energy models + và RNN models. + - Đồng bộ và không đồng bộ và không đồng bộ distributed training frameworks sử + dụng CPU + - Điểm kiểm tra (Checkpoint) và khôi phục + - Kiểm tra đơn vị sử dụng gtest diff --git a/docs-site/website/versioned_docs/version-4.3.0_Viet/examples.md b/docs-site/website/versioned_docs/version-4.3.0_Viet/examples.md new file mode 100644 index 0000000..601f5b8 --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0_Viet/examples.md @@ -0,0 +1,69 @@ +--- +id: version-4.2.0_Viet-examples +title: Ví Dụ +original_id: examples +--- + + + +Phần này đưa ra một vài ví dụ về việc thực hiện deep learning sử dụng SINGA. Mã +nguồn (source code) được cung cấp trong SINGA repo trên +[Github](https://github.com/apache/singa/tree/master/examples). Có thể tham khảo +các ví dụ sử dụng SINGA Python APIs trên CPU hoặc một GPU trên +[Google Colab](https://colab.research.google.com/). Bạn có thể trực tiếp chạy +thử trên Google Cloud mà không cần tạo local environment. Đường dẫn tới mỗi ví +dụ được cung cấp dưới đây. + +## Image Classification + +| Model | Dataset | Đường dẫn | +| ----------- | --------------------------------- | ------------------------------------------------------------------------------------------------------- | +| CNN cơ bản | MNIST, CIFAR10, CIFAR100 | [Colab](https://colab.research.google.com/drive/1fbGUs1AsoX6bU5F745RwQpohP4bHTktq) | +| AlexNet | ImageNet | [Cpp]() | +| VGG | ImageNet | [Cpp](), [Python](), [Colab](https://colab.research.google.com/drive/14kxgRKtbjPCKKsDJVNi3AvTev81Gp_Ds) | +| XceptionNet | MNIST, CIFAR10, CIFAR100 | [Python]() | +| ResNet | MNIST, CIFAR10, CIFAR100, CIFAR10 | [Python](), [Colab](https://colab.research.google.com/drive/1u1RYefSsVbiP4I-5wiBKHjsT9L0FxLm9) | +| MobileNet | ImageNet | [Colab](https://colab.research.google.com/drive/1HsixqJMIpKyEPhkbB8jy7NwNEFEAUWAf) | + +## Object Detection + +| Model | Dataset | Đường dẫn | +| ----------- | ---------- | ---------------------------------------------------------------------------------- | +| Tiny YOLOv2 | Pascal VOC | [Colab](https://colab.research.google.com/drive/11V4I6cRjIJNUv5ZGsEGwqHuoQEie6b1T) | + +## Nhận diện Khuôn mặt và Cảm xúc + +| Model | Dataset | Đường dẫn | +| --------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------ | ---------------------------------------------------------------------------------- | +| ArcFace | Refined MS-Celeb-1M | [Colab](https://colab.research.google.com/drive/1qanaqUKGIDtifdzEzJOHjEj4kYzA9uJC) | +| Emotion FerPlus | [Facial Expression Recognition Challenge](https://www.kaggle.com/c/challenges-in-representation-learning-facial-expression-recognition-challenge/data) | [Colab](https://colab.research.google.com/drive/1XHtBQGRhe58PDi4LGYJzYueWBeWbO23r) | + +## Image Generation + +| Model | Dataset | Đường dẫn | +| ----- | ------- | ---------------------------------------------------------------------------------- | +| GAN | MNIST | [Colab](https://colab.research.google.com/drive/1f86MNDW47DJqHoIqWD1tOxcyx2MWys8L) | +| LSGAN | MNIST | [Colab](https://colab.research.google.com/drive/1C6jNRf28vnFOI9JVM4lpkJPqxsnhxdol) | + +## Machine Comprehension + +| Model | Dataset | Đường dẫn | +| ---------- | ------------------------------------------------------------------------- | ---------------------------------------------------------------------------------- | +| Bert-Squad | [SQuAD v1.1](https://rajpurkar.github.io/SQuAD-explorer/explore/1.1/dev/) | [Colab](https://colab.research.google.com/drive/1kud-lUPjS_u-TkDAzihBTw0Vqr0FjCE-) | + +## Text Classification + +| Model | Dataset | Đường dẫn | +| ----------- | ------- | ---------- | +| Simple LSTM | IMDB | [python]() | + +## Text Ranking + +| Model | Dataset | Đường dẫn | +| ------ | ----------- | ---------- | +| BiLSTM | InsuranceQA | [python]() | + +## Misc. + +- Restricted Boltzmann Machine sử dụng dữ liệu MNIST, [nguồn](), + [Colab](https://colab.research.google.com/drive/19996noGu9JyHHkVmp4edBGu7PJSRQKsd). diff --git a/docs-site/website/versioned_docs/version-4.3.0_Viet/git-workflow.md b/docs-site/website/versioned_docs/version-4.3.0_Viet/git-workflow.md new file mode 100644 index 0000000..08af7c5 --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0_Viet/git-workflow.md @@ -0,0 +1,130 @@ +--- +id: version-4.2.0_Viet-git-workflow +title: Quy Trình Sử Dụng Git +original_id: git-workflow +--- + + + +## Dành cho Lập Trình Viên + +1. Fork [SINGA Github repository](https://github.com/apache/singa) về tài khoản + Github của bạn. + +2. Clone **repo** (viết tắt của repository) từ tài khoản Github của bạn + + ```shell + git clone https://github.com//singa.git + git remote add upstream https://github.com/apache/singa.git + ``` + +3. Tạo branch mới (vd., `feature-foo` hoặc `fixbug-foo`), chỉnh sửa và commit + code của bạn ở đây . + + ```shell + git checkout dev + git checkout -b feature-foo + # write your code + git add + git commit + ``` + + Nội dung lệnh commit nên bao gồm: + + - Tiêu đề (Title) mô tả. + - Mô tả chi tiết. Nếu lệnh commit là sửa lỗi (bugs), tốt nhất là nên bao gồm + việc mô tả ngắn gọn lại vấn đề. Nếu thêm tính năng mới, có thể bao gồm động + cơ thúc đẩy/mục đích của tính năng mới này. + + Nếu branch của bạn có nhiều commits nhỏ, bạn cần súc tích lại các commits + bằng cách + + ```shell + git rebase -i + ``` + + Bạn có thể + [squash và reword](https://help.github.com/en/articles/about-git-rebase) các + commits. + +4. Khi bạn đang xử lý các mã code, branch `dev` của SINGA có thể đang được cập + nhật bởi người khác; Trong trường hợp này, bạn cần pull dev mới nhất + + ```shell + git checkout dev + git pull upstream dev:dev + ``` + +5. [Rebase](https://git-scm.com/book/en/v2/Git-Branching-Rebasing) `feature-foo` + vào branch `dev` và push commits vào tài khoản Github của bạn ( branch mới). + Lệnh rebase nhằm giúp cho lịch sử commit của bạn rõ ràng. Các lệnh git dưới + đây nên được thực hiện sau khi bạn commit các việc làm của mình: + + ```shell + git checkout feature-foo + git rebase dev + git push origin feature-foo:feature-foo + ``` + + Lệnh rebase thực hiện các + [bước sau](https://git-scm.com/book/en/v2/Git-Branching-Rebasing): "Lệnh này + thực hiện bắt đầu từ hình thái ban đầu của hai branches (mà bạn đang sử dụng + hoặc bạn đang rebase vào), xác định sự khác nhau ở mỗi commit của branch bạn + đang sử dụng, lưu các điểm khác nhau vào tập tin tạm thời, điều chỉnh branch + hiện tại để có cùng commit với branch mà bạn đang rebase vào, rồi cuối cùng + áp dụng từng thay đổi một theo thứ tự." Bởi vậy, sau khi thực hiện, bạn sẽ + vẫn ở feature branch, nhưng commit IDs/hashes của bạn được thay đổi do các + điểm khác nhau đã được commit trong quá trình rebase; và branch của bạn giờ + đây chứa bản code cập nhật nhất từ branch dev và branch của bạn. + +6. Tạo một pull request (PR) vào branch dev của apache/singa trên website + Github. Nếu bạn muốn thông báo cho các thành viên khác đang làm việc trên + cùng một tập tin, bạn có thể tìm tập tin đó trên Github và nhấn vào "Blame" + để xem chú thích từng dòng một ai đã thay đổi code lần cuối cùng. Sau đó, bạn + có thể thêm @username trong mục mô tả PR để nhắc họ. Hãy nói rõ rằng những + đóng góp này là công sức của bạn và rằng bạn cấp bản quyền công sức này cho + dự án theo dạng bản quyền dự án mở. Những commits khác (vd., sửa lỗi) vào + branch mới này sẽ được tự động cập nhật vào pull request của bạn bởi Github. + +7. Đợi thành viên xét duyệt PR. Trong quá trình này, dev branch của SINGA có thể + được những người khác cập nhật, do vậy bạn cần phải + [merge the latest dev](https://docs.fast.ai/dev/git.html#how-to-keep-your-feature-branch-up-to-date) + để xử lý các conflicts. Một số người + [rebase PR vào branch dev mới nhất](https://github.com/edx/edx-platform/wiki/How-to-Rebase-a-Pull-Request) + thay vì merging. Tuy nhiên, nếu các thành viên khác fetch PR này để thêm các + tính năng mới rồi gửi PR, việc rebase sẽ gây ra **duplicate commits** (với + hash khác) ở PR mới. Xem + [Nguyên tắc vàng để Rebasing](https://www.atlassian.com/git/tutorials/merging-vs-rebasing) + để biết thêm chi tiết khi nào cần tránh rebase. Một giải pháp đơn giản để cập + nhật PR (nhằm xử lý conflicts hay lỗi commit) là checkout một branch mới từ + branch dev cập nhật nhất của Apache SINGAS repo; copy và paste các mã code + được cập nhật/thêm vào; commit và gửi một PR mới. + +## Dành cho commiters + +Commiters có thể merge pull requests (PRs) vào dev branch của repo upstream. +Trước khi merge mỗi PR, committer nên + +- kiểm tra thông điệp commit (nội dung và định dạng) +- kiểm tra những thay đổi so với code hiện tại. Thay đổi về API nên được ghi + lại. +- kiểm tra kết quả Travis testing cho định dạng mã code/tài liệu và unit tests + +Có hai cách để merge một pull request: + +- Trên Github. Làm theo [hướng dẫn](https://gitbox.apache.org/setup/) để kết nối + tài khoản Apache với tài khoản Github của bạn. Sau đó bạn có thể trực tiếp + merge PRs trên GitHub. +- Để merge pull request https://github.com/apache/singa/pull/xxx qua command + line, thực hiện theo hướng dẫn sau: + + ```shell + git clone https://github.com/apache/singa.git + git remote add asf https://gitbox.apache.org/repos/asf/singa.git + git fetch origin pull/xxx/head:prxxx + git checkout dev + git merge --no-ff prxxx + git push asf dev:dev + ``` + + Không sử dụng rebase để merge PR; và vô hiệu hoá fast forward. diff --git a/docs-site/website/versioned_docs/version-4.3.0_Viet/graph.md b/docs-site/website/versioned_docs/version-4.3.0_Viet/graph.md new file mode 100644 index 0000000..0003a96 --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0_Viet/graph.md @@ -0,0 +1,525 @@ +--- +id: version-4.2.0_Viet-graph +title: Model +original_id: graph +--- + + + +Forward và backward propagation trong mạng thần kinh nhân tạo (neural network) +có thể sử dụng một tập hợp các hàm như convolution và pooling. Mỗi hàm nhận một +vài input [tensors](./tensor) và áp dụng một [operator](./autograd) để tạo +output tensors. Bằng việc thể hiện mỗi operator là một node và mỗi tensor là một +edge, tất cả dạng hàm tạo thành một computational graph. Với computational +graph, tối ưu hoá tốc độ và bộ nhớ có thể được tiến hành bởi việc đưa vào thực +hiện việc phân bổ/giải phóng bộ nhớ và thao tác một cách hợp lý. Trong SINGA, +người dùng chỉ cần xác định neural network model sử dụng API của hàm +[Model](https://github.com/apache/singa/blob/master/python/singa/model.py). +Graph được xây dựng và tối ưu hoá ở C++ phía sau một cách tự động. + +Theo đó, một mặt người dùng thực hiện network sử dụng API của hàm +[Model](./graph) tuân theo phong cách lập trình bắt buộc như PyTorch. Có điều +khác với PyTorch phải tái tạo lại các thao tác ở mỗi vòng lặp, SINGA buffer các +thao tác để tạo computational graph một cách đầy đủ (khi tính năng này được kích +hoạt) sau vòng lặp đầu tiên. Do đó, mặt khác, SINGA có computational graph giống +như được tạo bởi các libraries sử dụng lập trình khai báo (declarative +programming), như TensorFlow. Nên nó được tối ưu hoá qua graph. + +## Ví Dụ + +Mã code sau mô phỏng việc sử dụng API của hàm `Model`. + +1. Áp dụng model mới như một tập con của Model class. + +```Python +class CNN(model.Model): + + def __init__(self, num_classes=10, num_channels=1): + super(CNN, self).__init__() + self.conv1 = layer.Conv2d(num_channels, 20, 5, padding=0, activation="RELU") + self.conv2 = layer.Conv2d(20, 50, 5, padding=0, activation="RELU") + self.linear1 = layer.Linear(500) + self.linear2 = layer.Linear(num_classes) + self.pooling1 = layer.MaxPool2d(2, 2, padding=0) + self.pooling2 = layer.MaxPool2d(2, 2, padding=0) + self.relu = layer.ReLU() + self.flatten = layer.Flatten() + self.softmax_cross_entropy = layer.SoftMaxCrossEntropy() + + def forward(self, x): + y = self.conv1(x) + y = self.pooling1(y) + y = self.conv2(y) + y = self.pooling2(y) + y = self.flatten(y) + y = self.linear1(y) + y = self.relu(y) + y = self.linear2(y) + return y + + def train_one_batch(self, x, y): + out = self.forward(x) + loss = self.softmax_cross_entropy(out, y) + self.optimizer(loss) + return out, loss +``` + +2. Tạo một instance cho model, optimizer, device, v.v. Compile model đó + +```python +model = CNN() + +# khởi tạo optimizer và đính nó vào model +sgd = opt.SGD(lr=0.005, momentum=0.9, weight_decay=1e-5) +model.set_optimizer(sgd) + +# khởi tạo device +dev = device.create_cuda_gpu() + +# input và target placeholders cho model +tx = tensor.Tensor((batch_size, 1, IMG_SIZE, IMG_SIZE), dev, tensor.float32) +ty = tensor.Tensor((batch_size, num_classes), dev, tensor.int32) + +# compile model trước khi training +model.compile([tx], is_train=True, use_graph=True, sequential=False) +``` + +3. Train model theo vòng lặp + +```python +for b in range(num_train_batch): + # tạo mini-batch tiếp theo + x, y = ... + + # Copy dữ liệu vào input tensors + tx.copy_from_numpy(x) + ty.copy_from_numpy(y) + + # Training với một batch + out, loss = model(tx, ty) +``` + +Ví dụ này có trên Google Colab notebook +[tại đây](https://colab.research.google.com/drive/1fbGUs1AsoX6bU5F745RwQpohP4bHTktq). + +Các ví dụ khác: + +- [MLP](https://github.com/apache/singa/blob/master/examples/mlp/model.py) +- [CNN](https://github.com/apache/singa/blob/master/examples/cnn/model/cnn.py) +- [ResNet](https://github.com/apache/singa/blob/master/examples/cnn/model/resnet.py) + +## Thực Hiện + +### Xây Dựng Graph + +SINGA tạo computational graph qua 3 bước: + +1. Buffer các thao tác +2. Phân tích hoạt động các thư viện sử dụng trong dự án (dependencies) +3. Tạo nodes và edges dựa trên dependencies + +Sử dụng phép nhân ma trận từ dense layer của +[MLP model](https://github.com/apache/singa/blob/master/examples/mlp/model.py) +làm ví dụ. Quá trình này gọi là hàm `forward` function của class MLP + +```python +class MLP(model.Model): + + def __init__(self, data_size=10, perceptron_size=100, num_classes=10): + super(MLP, self).__init__() + self.linear1 = layer.Linear(perceptron_size) + ... + + def forward(self, inputs): + y = self.linear1(inputs) + ... +``` + +Layer `Linear` tạo thành từ phép tính `mutmul`. `autograd` áp dụng phép `matmul` +bằng cách gọi hàm `Mult` được lấy từ CPP qua SWIG. + +```python +# áp dụng matmul() +singa.Mult(inputs, w) +``` + +Từ phía sau, hàm `Mult` function được áp dụng bằng cách gọi `GEMV`, là một hàm +CBLAS. thay vì gọi hàm `GEMV` trực tiếp, `Mult` gửi đi `GEMV` và đối số +(argument) tới thiết bị (device) như sau, + +```c++ +// Áp dụng Mult() +C->device()->Exec( + [a, A, b, B, CRef](Context *ctx) mutable { + GEMV(a, A, B, b, &CRef, ctx); + }, + read_blocks, {C->block()}); +``` + +Hàm `Exec` function của `Device` buffer hàm này và các đối số của nó. Thêm vào +đó, nó cũng có thông tin về các block (một block là một đoạn bộ nhớ cho một +tensor) để đọc và viết bởi hàm này. + +Sau khi `Model.forward()` được thực hiện xong một lần, tất cả quá trình được +buffer bởi `Device`. Tiếp theo, thông tin đọc/viết của tất cả quá trình sẽ được +phân tích để tạo computational graph. Ví dụ, nếu một block `b` được viết bởi quá +trình 01 và sau đó được đọc bởi quá trình 02 khác, chúng ta sẽ biết 02 là dựa +vào 01 và có edge trực tiếp từ A sang B, thể hiện qua block `b` (hoặc tensor của +nó). Sau đó một graph không tuần hoàn sẽ được tạo ra như dưới đây. Graph chỉ +được tạo ra một lần. + +![Computational graph của MLP](assets/GraphOfMLP.png) + +
**Sơ đồ 1 - Ví dụ Computational graph của MLP.** + +### Tối Ưu Hoá + +Hiện nay, các tối ưu hoá sau được thực hiện dựa trên computational graph. + +**Phân bổ thụ động (Lazy allocation)** Khi tensor/blocks được tạo ra, các thiết +bị (devices) không phân bổ bộ nhớ cho chúng ngay lập tức. Thay vào đó, khi block +được tiếp cận lần đầu tiên, bộ nhớ sẽ được phân bổ. + +**Tự động tái sử dụng (Automatic recycling)** Đếm số của mỗi tensor/block được +tính dựa trên graph. Trước khi thực hiện quá trình nào, đếm số là số lượng hàm +đọc block này. Trong quá trình thực hiện, khi một hàm nào được tiến hành, đếm số +của mỗi block đầu vào bị trừ đi 1. Nếu đếm số của một block bằng 0, thì block +này sẽ không được đọc lại nữa trong toàn bộ quá trình còn lại. Bởi vậy, bộ nhớ +của nó được giải phóng một cách an toàn. Thêm vào đó, SINGA theo dõi việc sử +dụng block bên ngoài graph. Nếu block được sử dụng bởi mã code Python (không +phải các hàm autograd), nó sẽ không được tái sử dụng. + +**Chia sẻ bộ nhớ** SINGA sử dụng memory pool, như là +[CnMem](https://github.com/NVIDIA/cnmem) để quản lý bộ nhớ CUDA. Với _Automatic +recycling_ và memory pool, SINGA có thể chia sẻ bộ nhớ giữa các tensor. Xem xét +hai hàm `c = a + b` và `d=2xc`. Trước khi thực hiện hàm thứ hai, theo như _Lazy +allocation_ thì bộ nhớ của d nên được sử dụng. Cũng như `a` không được sử dụng ở +toàn bộ quá trình còn lại. Theo Tự động sử dụng (Automatic recycling), block của +`a` sẽ được giải phóng sau hàm đầu tiên. Vì thế, SINGA sẽ đề xuất bốn hàm tới +CUDA stream: addition, free `a`, malloc `b`, và multiplication. Memory pool sau +đó có thể chia sẻ bộ nhớ được `a` với `b` giải phóng thay vì yêu cầu GPU thực +hiện real malloc cho `b`. + +Các kĩ thuật tối ưu hoá khác, ví dụ từ compliers, như common sub-expression +elimination và parallelizing operations trên CUDA streams khác nhau cũng có thể +được áp dụng. + +## Toán Tử (Operator) mới + +Mỗi toán tử được định nghĩa trong `autograd` module áp dụng hai hàm: forward và +backward, được thực hiện bằng cách gọi toán tử (operator) từ backend. Để thêm +một toán tử mới vào hàm `autograd`, bạn cần thêm nhiều toán tử ở backend. + +Lấy toán tử +[Conv2d](https://github.com/apache/singa/blob/master/python/singa/autograd.py) +làm ví dụ, từ phía Python, hàm forward và backward được thực hiện bằng cách gọi +các toán tử từ backend dựa trên loại device. + +```python +class _Conv2d(Operation): + + def forward(self, x, W, b=None): + ...... + if training: + if self.handle.bias_term: + self.inputs = (x, W, b) # ghi chép x, W, b + else: + self.inputs = (x, W) + + if (type(self.handle) != singa.ConvHandle): + return singa.GpuConvForward(x, W, b, self.handle) + else: + return singa.CpuConvForward(x, W, b, self.handle) + + def backward(self, dy): + if (type(self.handle) != singa.ConvHandle): + dx = singa.GpuConvBackwardx(dy, self.inputs[1], self.inputs[0], + self.handle) + dW = singa.GpuConvBackwardW(dy, self.inputs[0], self.inputs[1], + self.handle) + db = singa.GpuConvBackwardb( + dy, self.inputs[2], + self.handle) if self.handle.bias_term else None + else: + dx = singa.CpuConvBackwardx(dy, self.inputs[1], self.inputs[0], + self.handle) + dW = singa.CpuConvBackwardW(dy, self.inputs[0], self.inputs[1], + self.handle) + db = singa.CpuConvBackwardb( + dy, self.inputs[2], + self.handle) if self.handle.bias_term else None + if db: + return dx, dW, db + else: + return dx, dW +``` + +Mỗi toán tử ở backend nên được thực hiện theo cách sau: + +- Giả dụ toán từ là `foo()`; khi được thực hiện nên được gói vào trong một hàm + khác, như `_foo()`. `foo()` chuyển `_foo` cùng với các đối số như một hàm + lambda tới hàm `Device`'s `Exec` để buffer. Block để đọc và viết cũng được + chuyển cho `Exec`. + +- Tất cả đối số được sử dụng trong hàm lambda expression cần phải được thu thập + dựa trên các nguyên tắc sau. + + - `thu thập bằng giá trị`: Nếu biến đối số (argument variable) là biến local + hoặc sẽ được giải phóng ngay (như intermediate tensors). Hoặc, những biến số + này sẽ bị loại bỏ khi `foo()` tồn tại. + - `thu thập theo tham khảo`:Nếu biến số được ghi lại từ phía python hoặc một + biến bất biến (như tham số W và ConvHand trong Conv2d class). + + - `mutable`: Biểu thức lambda expression nên có biến thẻ (mutable tag) nếu một + biến được thu thập theo giá trị bị thay đổi trong hàm `_foo()` + +Đây là một +[ví dụ](https://github.com/apache/singa/blob/master/src/model/operation/convolution.cc) +về operator được áp dụng ở backend. + +```c++ +Tensor GpuConvBackwardx(const Tensor &dy, const Tensor &W, const Tensor &x, + const CudnnConvHandle &cch) { + CHECK_EQ(dy.device()->lang(), kCuda); + + Tensor dx; + dx.ResetLike(x); + + dy.device()->Exec( + /* + * dx is a local variable so it's captured by value + * dy is an intermediate tensor and isn't recorded on the python side + * W is an intermediate tensor but it's recorded on the python side + * chh is a variable and it's recorded on the python side + */ + [dx, dy, &W, &cch](Context *ctx) mutable { + Block *wblock = W.block(), *dyblock = dy.block(), *dxblock = dx.block(); + float alpha = 1.f, beta = 0.f; + cudnnConvolutionBackwardData( + ctx->cudnn_handle, &alpha, cch.filter_desc, wblock->data(), + cch.y_desc, dyblock->data(), cch.conv_desc, cch.bp_data_alg, + cch.workspace.block()->mutable_data(), + cch.workspace_count * sizeof(float), &beta, cch.x_desc, + dxblock->mutable_data()); + }, + {dy.block(), W.block()}, {dx.block(), cch.workspace.block()}); + /* the lambda expression reads the blocks of tensor dy and w + * and writes the blocks of tensor dx and chh.workspace + */ + + return dx; +} +``` + +## Điểm Chuẩn (Benchmark) + +### Trên một node + +- Thiết lập thí nghiệm + - Model + - Sử dụng layer: ResNet50 trong + [resnet.py](https://github.com/apache/singa/blob/master/examples/cnn/autograd/resnet_cifar10.py) + - Sử dụng model: ResNet50 trong + [resnet.py](https://github.com/apache/singa/blob/master/examples/cnn/model/resnet.py) + - GPU: NVIDIA RTX 2080Ti +- Kí hiệu + - `s` :giây (second) + - `it` : vòng lặp (iteration) + - `Mem`:sử dụng bộ nhớ tối đa trong một GPU + - `Throughout`:số lượng hình ảnh được xử lý mỗi giây + - `Time`:tổng thời gian + - `Speed`:vòng lặp mỗi giây + - `Reduction`:tốc độ giảm bộ nhớ sử dụng so với sử dụng layer + - `Speedup`: tốc độ tăng tốc so với dev branch +- Kết quả + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
BatchsizeCasesMem(MB)Time(s)Speed(it/s)ThroughputReductionSpeedup
16layer497514.195214.0893225.42850.00%1.0000
model:disable graph499514.126414.1579226.5261-0.40%1.0049
model:enable graph, bfs328313.743814.5520232.831834.01%1.0328
model:enable graph, serial326513.742014.5540232.863534.37%1.0330
32layer1011913.45877.4302237.76490.00%1.0000
model:disable graph1010913.29527.5315240.68750.10%1.0123
model:enable graph, bfs683913.10597.6302244.164832.41%1.0269
model:enable graph, serial684513.04897.6635245.231232.35%1.0314
+ +### Đa quá trình (Multi processes) + +- Thiết lập thí nghiệm + - API + - Sử dụng Layer: ResNet50 trong + [resnet_dist.py](https://github.com/apache/singa/blob/master/examples/cnn/autograd/resnet_dist.py) + - Sử dụng Model: ResNet50 trong + [resnet.py](https://github.com/apache/singa/blob/master/examples/cnn/model/resnet.py) + - GPU: NVIDIA RTX 2080Ti \* 2 + - MPI: hai quá trình MPI trên một node +- Kí hiệu: như trên +- kết quả + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
BatchsizeCasesMem(MB)Time(s)Speed(it/s)ThroughputReductionSpeedup
16layer543917.332311.5391369.25220.00%1.0000
model:disable graph542717.823211.2213359.08310.22%0.9725
model:enable graph, bfs338918.231010.9703351.050437.69%0.9507
model:enable graph, serial343717.038911.7378375.610336.81%1.0172
32layer1054714.86356.7279430.58580.00%1.0000
model:disable graph1050314.77466.7684433.17480.42%1.0060
model:enable graph, bfs693514.85536.7316430.823134.25%1.0006
model:enable graph, serial702714.32716.9798446.707433.37%1.0374
+ +### Kết Luận + +- Training với computational graph giúp giảm đáng kể khối bộ nhớ. +- Hiện tại, tốc độ có cải thiện một chút. Nhiều tối ưu hoá có thể được thực hiện + giúp tăng hiệu quả. diff --git a/docs-site/website/versioned_docs/version-4.3.0_Viet/history-singa.md b/docs-site/website/versioned_docs/version-4.3.0_Viet/history-singa.md new file mode 100644 index 0000000..f61fff5 --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0_Viet/history-singa.md @@ -0,0 +1,43 @@ +--- +id: version-4.2.0_Viet-history-singa +title: Lịch Sử Phát Triển SINGA +original_id: history-singa +--- + + + +## Lịch Sử Phát Triển + +SINGA được phát triển bởi DB System Group tại ĐH Quốc Gia Singapore (NUS) vào +năm 2014, hợp tác với nhóm nghiên cứu cơ sở dữ liệu của ĐH Chiết Giang. Vui lòng +cite các báo cáo khoa học dưới đây nếu bạn muốn sử dụng SINGA trong nghiên cứu +của mình: + +- B.C. Ooi, K.-L. Tan, S. Wang, W. Wang, Q. Cai, G. Chen, J. Gao, Z. Luo, A. K. + H. Tung, Y. Wang, Z. Xie, M. Zhang, and K. Zheng. + [SINGA: A distributed deep learning platform](http://www.comp.nus.edu.sg/~ooibc/singaopen-mm15.pdf). + ACM Multimedia (Open Source Software Competition) 2015 + +- W. Wang, G. Chen, T. T. A. Dinh, B. C. Ooi, K.-L.Tan, J. Gao, and S. Wang. + [SINGA: putting deep learning in the hands of multimedia users](http://www.comp.nus.edu.sg/~ooibc/singa-mm15.pdf). + ACM Multimedia 2015. + +Rafiki là một mô-đun của SINGA. Vui lòng cite các báo cáo khoa học dưới đây nếu +bạn muốn sử dụng Rafiki trong nghiên cứu của mình: + +- Wei Wang, Jinyang Gao, Meihui Zhang, Sheng Wang, Gang Chen, Teck Khim Ng, Beng + Chin Ooi, Jie Shao, Moaz Reyad. + [Rafiki: Machine Learning as an Analytics Service System](http://www.vldb.org/pvldb/vol12/p128-wang.pdf). + [VLDB 2019](http://vldb.org/2019/) + ([BibTex](https://dblp.org/rec/bib2/journals/pvldb/WangWGZCNOS18.bib)). + +Các công ty như [NetEase](http://tech.163.com/17/0602/17/CLUL016I00098GJ5.html), +[yzBigData](http://www.yzbigdata.com/en/index.html), +[Shentilium](https://shentilium.com/), [Foodlg](http://www.foodlg.com/) and +[Medilot](https://medilot.com/technologies) đang sử dụng SINGA cho các ứng dụng +của họ. + +## Bản Quyền + +SINGA được phát hành bởi +[Bản Quyền Apache phiên bản 2.0](http://www.apache.org/licenses/LICENSE-2.0) diff --git a/docs-site/website/versioned_docs/version-4.3.0_Viet/how-to-release.md b/docs-site/website/versioned_docs/version-4.3.0_Viet/how-to-release.md new file mode 100644 index 0000000..2197f4f --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0_Viet/how-to-release.md @@ -0,0 +1,207 @@ +--- +id: version-4.2.0_Viet-how-to-release +title: Chuẩn bị trước khi phát hành +original_id: how-to-release +--- + + + +Đây là hướng dẫn chuẩn bị cho việc phát hành +[quá trình chuẩn bị trước khi phát hành](http://www.apache.org/dev/release-publishing.html) +SINGA. + +1. Lựa chọn người quản lý cho việc phát hành. Người quản lý chịu trách nhiệm + điều phối quá trình phát hành. Chữ ký của người quản lý (.asc) sẽ được tải + lên cùng với bản phát hành. Nggười quản lý tạo KEY (RSA 4096-bit) và tải nó + lên public key server. Để được tin cậy kết nối trên web, người quản lý cần + các người dùng Apache khác chứng thực (signed) Key của mình. Anh ta trước + tiên cần yêu cầu mentor giúp chứng thực key. + [Cách tạo Key](http://www.apache.org/dev/release-signing.html)? + +2. Kiểm tra bản quyền. + [FAQ](https://www.apache.org/legal/src-headers.html#faq-docs); + [Các bản SINGA đã phát hành](https://issues.apache.org/jira/projects/SINGA/issues/SINGA-447) + + - Nền tảng code không bao gồm code của bên thứ 3 mà không tương thích với + APL; + - Các chương trình dependencies phải tương thích với APL. Các licenses giống + với GNU là không tương thích; + - Các tệp tin nguồn viết bởi chúng tôi PHẢI bao gồm license header của + Apache: http://www.apache.org/legal/src-headers.html. Chúng tôi cung cấp + script để chạy header trên tất cả các tệp tin. + - Cập nhật tệp tin LICENSE. Nếu code có chứa mã code của một bên thứ 3 trong + bản phát hành mà không phải APL, phải nêu rõ ở phần cuối của tập tin THÔNG + BÁO. + +3. Nâng cấp phiên bản. Kiểm tra mã code và Tài liệu hướng dẫn + + - Quá trình cài đặt không bị lỗi nào. + - Bao gồm tests cho những mục nhỏ (nhiều nhất có thể) + - Gói chương trình Conda chạy không bị lỗi. + - Tài liệu hướng dẫn trực tuyến trên trang web Apache là mới nhất. + +4. Chuẩn bị tệp tin RELEASE_NOTES (Lưu ý phát hành). Bao gồm các mục, Giới + thiệu, Tính năng nổi bật, Lỗi Bugs, (đường dẫn tới JIRA hoặc Github PR), + Những thay đổi, Danh sách thư viện Dependency, Các vấn đề không tương thích. + Làm theo + [ví dụ](http://commons.apache.org/proper/commons-digester/commons-digester-3.0/RELEASE-NOTES.txt). + +5. Gói các phiên bản phát hành. Bản phát hành cần được gói gọn thành: + apache-singa-VERSION.tar.gz. Trong bản phát hành không nên chứa bất kì tệp + tin dạng binary nào, bao gồm cả các tệp tin git. Tuy nhiên, các tệp CMake + compilation dựa vào git tag để tạo số phiên bản; để bỏ qua dependency này, + bạn cần cập nhật tệp tin CMakeLists.txt theo cách thủ công để tạo số phiên + bản. + + ``` + # xoá các dòng sau + include(GetGitRevisionDescription) + git_describe(VERSION --tags --dirty=-d) + string(REGEX REPLACE "^([0-9]+)\\..*" "\\1" VERSION_MAJOR "${VERSION}") + string(REGEX REPLACE "^[0-9]+\\.([0-9]+).*" "\\1" VERSION_MINOR "${VERSION}") + string(REGEX REPLACE "^[0-9]+\\.[0-9]+\\.([0-9]+).*" "\\1" VERSION_PATCH "${VERSION}") + + # thay đổi số + SET(PACKAGE_VERSION 3.0.0) + SET(VERSION 3.0.0) + SET(SINGA_MAJOR_VERSION 3) # 0 - + SET(SINGA_MINOR_VERSION 0) # 0 - 9 + SET(SINGA_PATCH_VERSION 0) # 0 - 99 + ``` + + Tải gói chương trình lên + [stage repo](https://dist.apache.org/repos/dist/dev/singa/). Cần bao gồm các + tệp tin tar, signature, KEY và tệp tin SHA256 checksum. Không sử dụng MD5. + Xem chính sách tại + [đây](http://www.apache.org/dev/release-distribution#sigs-and-sums). Thư mục + stage cần bao gồm: + + - apache-singa-VERSION.tar.gz + - apache-singa-VERSION.acs + - apache-singa-VERSION.SHA256 + + Các lệnh để tạo tệp tin và tải chúng lên stage svn repo: + + ```sh + # trong singa repo + rm -rf .git + rm -rf rafiki/* + cd .. + tar -czvf apache-singa-VERSION.tar.gz singa/ + + mkdir stage + cd stage + svn co https://dist.apache.org/repos/dist/dev/singa/ + cd singa + # copy tệp tin KEYS từ singa repo sang thư mục này nếu không có + cp ../../singa/KEYS . + mkdir VERSION + # copy tệp tin tar.gz + mv ../../apache-singa-VERSION.tar.gz VERSION/ + cd VERSION + sha512sum apache-singa-VERSION.tar.gz > apache-singa-VERSION.tar.gz.sha512 + gpg --armor --output apache-singa-VERSION.tar.gz.asc --detach-sig apache-singa-VERSION.tar.gz + cd .. + svn add VERSION + svn commit + ``` + +6) Kêu gọi vote bằng cách gửi email. Xem ví dụ dưới đây. + + ``` + To: dev@singa.apache.org + Subject: [VOTE] Release apache-singa-X.Y.Z (release candidate N) + + Hi all, + + I have created a build for Apache SINGA 3.1.0, release candidate 2. + + The release note is at + https://github.com/apache/singa/blob/master/RELEASE_NOTES. + + The artifacts to be voted on are located here: + https://dist.apache.org/repos/dist/dev/singa/3.1.0.rc2/apache-singa-3.1.0.rc2.tar.gz +   + The hashes of the artifacts are as follows: + SHA512: 84545499ad36da108c6a599edd1d853f82d331bc03273b5278515554866f0c698e881f956b2eabcb6b29c07fa9fa4ff1add5a777b58db8a6a2362cf383b5c04d  + + Release artifacts are signed with the followingkey: + https://dist.apache.org/repos/dist/dev/singa/KEYS + + The signature file is: + https://dist.apache.org/repos/dist/dev/singa/3.1.0.rc2/apache-singa-3.1.0.rc2.tar.gz.asc + + The Github tag is at: + https://github.com/apache/singa/releases/tag/3.1.0.rc2 + + The documentation website is at + http://singa.apache.org/docs/next/installation/ + + Some examples are available for testing: + https://github.com/apache/singa/tree/master/examples + ``` + +Please vote on releasing this package. The vote is open for at least 72 hours +and passes if a majority of at least three +1 votes are cast. + +[ ] +1 Release this package as Apache SINGA X.Y.Z [ ] 0 I don't feel strongly +about it, but I'm okay with the release [ ] -1 Do not release this package +because... + +Here is my vote: +1 + +``` + +7) Sau đó đợi ít nhất 48 giờ để nhận phản hồi. Bất kì PMC, committer hay contributor +đều có thể kiểm tra các tính năng trước khi phát hành, và đưa ra nhận xét. Mọi người nên kiểm tra trước khi +đưa ra vote +1. Nếu vote được thông qua, vui lòng gửi email kết quả. Nếu không thì cần lặp lại trình tự từ đầu. + +``` + +To: dev@singa.apache.org Subject: [RESULT][vote] Release apache-singa-X.Y.Z +(release candidate N) + +Thanks to everyone who has voted and given their comments. The tally is as +follows. + +N binding +1s: + +N non-binding +1s: + +No 0s or -1s. + +I am delighted to announce that the proposal to release Apache SINGA X.Y.Z has +passed. + +```` + +8) Tải gói chương trình để +[phân bổ](http://www.apache.org/dev/release-publishing.html#distribution) +tới https://dist.apache.org/repos/dist/release/singa/. + +9) Cập nhật trang Tải (Download) trên website SINGA. Tệp tin tar.gz PHẢI được tải từ mirror, sử dụng closer.cgi script; các tạo tác khác PHẢI được tải từ trang chủ Apache. Xem chi tiết tại +[đây](http://www.apache.org/dev/release-download-pages.html). Một vài nhận xét chúng tôi nhận được trong các đợt phát hành trước: "Trang Tải chỉ nên được dẫn tới các bản phát hành chính thức, vì vậy phải bao gồm đường dẫn tới GitHub.", "Đường dẫn tới KEYS, sigs và +hashes không nên sử dụng dist.apache.org; mà nên dùng +https://www.apache.org/dist/singa/...;", "Và bạn chỉ cần một đường dẫn tới KEYS, +và cần có hướng dẫn cách sử dụng KEYS + sig hay hash để chứng thực hoàn tất việc tải." + +10) Xoá tag RC và tập hợp gói conda packages. + +11) Xuất bản thông tin phát hành. + + ``` + To: announce@apache.org, dev@singa.apache.org + Subject: [ANNOUNCE] Apache SINGA X.Y.Z released + + We are pleased to announce that SINGA X.Y.Z is released. + + SINGA is a general distributed deep learning platform + for training big deep learning models over large datasets. + The release is available at: http://singa.apache.org/downloads.html + The main features of this release include XXX + We look forward to hearing your feedback, suggestions, + and contributions to the project. + + On behalf of the SINGA team, {SINGA Team Member Name} + ``` +```` diff --git a/docs-site/website/versioned_docs/version-4.3.0_Viet/install-win.md b/docs-site/website/versioned_docs/version-4.3.0_Viet/install-win.md new file mode 100644 index 0000000..ccb9f74 --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0_Viet/install-win.md @@ -0,0 +1,396 @@ +--- +id: version-4.2.0_Viet-install-win +title: Cách cài SINGA trên Windows +original_id: install-win +--- + + + +Quá trình cài đặt SINGA từ nguồn sử dụng Microsoft Windows bao gồm bốn bước: cài +đặt thư viện dependencies, cài SINGA từ nguồn, (không bắt buộc) cài đặt python +module và (không bắt buộc) chạy thử unit tests. + +## Cài đặt thư viện dependencies + +Bạn có thể tạo một thư mục để cài đặt thư viện dependencies. + +Các thư viện dependencies bao gồm: + +- Compiler và IDE + - Visual Studio. Công cụ biên tập mã này miễn phí và có thể được dùng trong + việc cài đặt SINGA. https://www.visualstudio.com/ +- CMake + - Có thể tải về qua http://cmake.org/ + - Đảm bảo đường dẫn khả thi của cmake nằm trong đường dẫn chương trình system + path, hoặc sử dụng đường dẫn đầy đủ khi gọi hàm cmake. +- SWIG + + - Có thể tải từ http://swig.org/ + - Đảm bảo đường dẫn khả thi của swig nằm trong đường dẫn chương trình system + path, hoặc sử dụng đường dẫn đầy đủ khi gọi hàm swig. Sử dụng các phiên bản + cập nhật như 3.0.12. + +- Protocol Buffers + - Tải các phiên bản phù hợp như 2.6.1: + https://github.com/google/protobuf/releases/tag/v2.6.1 . + - Tải cả hai tệp protobuf-2.6.1.zip và protoc-2.6.1-win32.zip . + - Giải nén cả hai tệp trên trong thư mục thư viện dependencies. Thêm đường dẫn + khả thi cho protoc vào system path, hoặc sử dụng đường dẫn đầy đủ khi gọi + hàm này. + - Mở Visual Studio solution có thể tìm trong thư mục vsproject. + - Thay đổi cài đặt thiết lập Settings tới Release and x64. + - Cài đặt libprotobuf project. +- Openblas + + - Tải phiên bản nguồn phù hợp như 0.2.20 từ http://www.openblas.net + - Giải nén nguồn trong thư mục thư viện dependencies. + - Nếu bạn không có chương trình Perl, tải perl environment như Strawberry Perl + (http://strawberryperl.com/) + - Cài đặt Visual Studio solution bằng lệnh sau từ thư mục nguồn: + + ```bash + cmake -G "Visual Studio 15 2017 Win64" + ``` + + - Mở Visual Studio solution và thay đổi cấu hình cài đặt cho Release and x64. + - Cài libopenblas project + +- Google glog + - Tải phiên bản phù hợp như 0.3.5 từ https://github.com/google/glog/releases + - Giải nén nguồn trong thư mục thư viện dependencies. + - Mở Visual Studio solution. + - Thay đổi cài đặt thiết lập Settings tới Release and x64. + - Cài đặt libglog project + +## Cài SINGA từ nguồn + +- Tải code nguồn của SINGA +- Cấu tạo các tệp protobuf: + + - Tới thư mục src/proto + + ```shell + mkdir python_out + protoc.exe *.proto --python_out python_out + ``` + +- Tạo swig interfaces cho C++ và Python: Tới mục src/api + + ```shell + swig -python -c++ singa.i + ``` + +- Tạo Visual Studio solution cho SINGA: Đi tới thư mục nguồn SINGA + + ```shell + mkdir build + cd build + ``` + +- Gọi hàm cmake và thêm đường dẫn vào trong system của bạn, tương tự như ví dụ + sau: + + ```shell + cmake -G "Visual Studio 15 2017 Win64" ^ + -DGLOG_INCLUDE_DIR="D:/WinSinga/dependencies/glog-0.3.5/src/windows" ^ + -DGLOG_LIBRARIES="D:/WinSinga/dependencies/glog-0.3.5/x64/Release" ^ + -DCBLAS_INCLUDE_DIR="D:/WinSinga/dependencies/openblas-0.2.20/lapack-netlib/CBLAS/include" ^ + -DCBLAS_LIBRARIES="D:/WinSinga/dependencies/openblas-0.2.20/lib/RELEASE" ^ + -DProtobuf_INCLUDE_DIR="D:/WinSinga/dependencies/protobuf-2.6.1/src" ^ + -DProtobuf_LIBRARIES="D:/WinSinga/dependencies/protobuf-2.6.1/vsprojects/x64/Release" ^ + -DProtobuf_PROTOC_EXECUTABLE="D:/WinSinga/dependencies/protoc-2.6.1-win32/protoc.exe" ^ + .. + ``` + +- Mở generated solution trong Visual Studio +- Thay đổi cài đặt thiết lập Settings tới Release and x64 +- Thêm tệp tin singa_wrap.cxx từ src/api tới singa_objects project +- Trong singa_objects project, mở Additional Include Directories. +- Thêm Python bao gồm đường dẫn +- Thêm numpy bao gồm đường dẫn +- Thêm protobuf bao gồm đường dẫn +- Trong định nghĩa preprocessor của singa_objects project, thêm USE_GLOG +- Sử dụng singa_objects project + +- Trong singa project: + + - thêm singa_wrap.obj vào Thư viện Object + - đổi tên mục target thành \_singa_wrap + - đổi định dạng tệp target thành .pyd + - đổi định dạng cấu hình sang Dynamic Library (.dll) + - đi tới Additional Library Directories và thêm đường dẫn vào các thư viện + python, openblas, protobuf và glog + - đi tới các thư viện Dependencies bổ sung để thêm libopenblas.lib, + libglog.lib và libprotobuf.lib + +- tạo singa project + +## Cài đặt Python module + +- Đổi `_singa_wrap.so` thành `_singa_wrap.pyd` trong build/python/setup.py +- Copy các tệp tin trong `src/proto/python_out` sang `build/python/singa/proto` + +- Không bắt buộc, tạo và kích hoạt virtual environment: + + ```shell + mkdir SingaEnv + virtualenv SingaEnv + SingaEnv\Scripts\activate + ``` + +- tới thư mục build/python và chạy: + + ```shell + python setup.py install + ``` + +- Sử dụng \_singa_wrap.pyd, libglog.dll và libopenblas.dll bằng cách thêm chúng + vào đường dẫn hoặc copy chúng vào thư mục gói chương trình singa trong gói + python site-packages + +- Xác nhận SINGA đã được cài đặt bằng cách chạy: + + ```shell + python -c "from singa import tensor" + ``` + +Tham khảo video quá trình cài đặt tại đây: + +[![youtube video](https://img.youtube.com/vi/cteER7WeiGk/0.jpg)](https://www.youtube.com/watch?v=cteER7WeiGk) + +## Chạy Unit Tests + +- Trong thư mục test, tạo Visual Studio solution: + + ```shell + cmake -G "Visual Studio 15 2017 Win64" + ``` + +- Mở generated solution trong Visual Studio. + +- Thay đổi cài đặt thiết lập Settings tới Release and x64. + +- Tạo glog project. + +- Trong mục test_singa project: + + - Thêm USE_GLOG vào Định nghĩa Preprocessor. + - Trong Additional Include Directories, thêm đường dẫn của GLOG_INCLUDE_DIR, + CBLAS_INCLUDE_DIR và Protobuf_INCLUDE_DIR đã được dùng ở bước 2 bên trên. + Đồng thời tạo và tạo/bao gồm các thư mục. + - Đi tới Additional Library Directories và thêm đường dẫn vào thư viện + openblas, protobuf và glog. Thêm build/src/singa_objects.dir/Release. + - Tới Thư viện Dependencies bổ sung và thêm libopenblas.lib, libglog.lib và + libprotobuf.lib. Sửa tên của hai thư viện: gtest.lib và singa_objects.lib. + +- Cài test_singa project. + +- Sử dụng libglog.dll và libopenblas.dll bằng cách thêm chúng vào đường dẫn hoặc + copy chúng vào thư mục test/release. + +- Tiến hành unit tests bằng cách + + - Từ dòng lệnh: + + ```shell + test_singa.exe + ``` + + - Từ Visual Studio: + - ấn chuột phải tại test_singa project và chọn 'Set as StartUp Project'. + - Từ mục Debug menu, chọn 'Start Without Debugging' + +Tham khảo video hướng dẫn chạy unit tests tại đây: + +[![youtube video](https://img.youtube.com/vi/393gPtzMN1k/0.jpg)](https://www.youtube.com/watch?v=393gPtzMN1k) + +## Cài đặt hỗ trợ GPU với CUDA + +Trong mục này, chúng tôi sẽ mở rộng các bước phía trên để sử dụng GPU. + +### Cài đặt thư viện Dependencies + +Ngoài các thư viện dependencies ở mục 1 phía trên, chúng ta cần: + +- CUDA + + Tải phiên bản phù hợp như 9.1 từ https://developer.nvidia.com/cuda-downloads . + Đảm bảo bạn cài đặt Visual Studio integration module. + +- cuDNN + + Tải phiên bản phù hợp như 7.1 từ https://developer.nvidia.com/cudnn + +- cnmem: + + - Tải phiên bản mới nhất tại https://github.com/NVIDIA/cnmem + - Tạo Visual Studio solution: + + ```shell + cmake -G "Visual Studio 15 2017 Win64" + ``` + + - Mở generated solution trong Visual Studio. + - Thay đổi cài đặt thiết lập Settings tới Release and x64. + - Tạo cnmem project. + +### Cài SINGA từ nguồn + +- Gọi hàm cmake và thêm đường dẫn vào trong system của bạn, tương tự như ví dụ + sau: + ```shell + cmake -G "Visual Studio 15 2017 Win64" ^ + -DGLOG_INCLUDE_DIR="D:/WinSinga/dependencies/glog-0.3.5/src/windows" ^ + -DGLOG_LIBRARIES="D:/WinSinga/dependencies/glog-0.3.5/x64/Release" ^ + -DCBLAS_INCLUDE_DIR="D:/WinSinga/dependencies/openblas-0.2.20/lapack-netlib/CBLAS/include" ^ + -DCBLAS_LIBRARIES="D:/WinSinga/dependencies/openblas-0.2.20/lib/RELEASE" ^ + -DProtobuf_INCLUDE_DIR="D:/WinSinga/dependencies/protobuf-2.6.1/src" ^ + -DProtobuf_LIBRARIES="D:\WinSinga/dependencies/protobuf-2.6.1/vsprojects/x64/Release" ^ + -DProtobuf_PROTOC_EXECUTABLE="D:/WinSinga/dependencies/protoc-2.6.1-win32/protoc.exe" ^ + -DCUDNN_INCLUDE_DIR=D:\WinSinga\dependencies\cudnn-9.1-windows10-x64-v7.1\cuda\include ^ + -DCUDNN_LIBRARIES=D:\WinSinga\dependencies\cudnn-9.1-windows10-x64-v7.1\cuda\lib\x64 ^ + -DSWIG_DIR=D:\WinSinga\dependencies\swigwin-3.0.12 ^ + -DSWIG_EXECUTABLE=D:\WinSinga\dependencies\swigwin-3.0.12\swig.exe ^ + -DUSE_CUDA=YES ^ + -DCUDNN_VERSION=7 ^ + .. + ``` + +* Tạo swig interfaces cho C++ và Python: Tới mục src/api + + ```shell + swig -python -c++ singa.i + ``` + +* Mở generated solution trong Visual Studio + +* Thay đổi cài đặt thiết lập Settings tới Release and x64. + +#### Tạo singa_objects + +- Thêm tệp tin singa_wrap.cxx từ src/api tới singa_objects project +- Trong singa_objects project, mở Additional Include Directories. +- Thêm Python bao gồm đường dẫn +- Thêm numpy bao gồm đường dẫn +- Thêm protobuf bao gồm đường dẫn +- Bổ sung bao gồm đường dẫn cho CUDA, cuDNN và cnmem +- Trong định nghĩa preprocessor của singa_objects project, thêm USE_GLOG, + USE_CUDA và USE_CUDNN. Xoá DISABLE_WARNINGS. +- Tạo singa_objects project + +#### Tạo singa-kernel + +- Tạo một Visual Studio project mới dưới dạng "CUDA 9.1 Runtime". Đặt tên dạng + như singa-kernel. +- project này chứa sẵn một tệp tin là kernel.cu. Xoá tệp tin này khỏi project. +- Thêm tệp tin này: src/core/tensor/math_kernel.cu +- Trong mục cài đặt project: + + - Đặt Platform Toolset sang dạng "Visual Studio 2015 (v140)" + - Đổi Configuration Type sang " Static Library (.lib)" + - Trong mục Include Directories, thêm vào build/include. + +- Tạo singa-kernel project + +#### Cài đặt singa + +- Trong singa project: + + - thêm singa_wrap.obj vào Object Libraries + - đổi tên target thành \_singa_wrap + - đổi định dạng target sang .pyd + - đổi định dạng cấu hình sang Dynamic Library (.dll) + - đi tới Additional Library Directories và thêm đường dẫn vào các thư viện + python, openblas, protobuf và glog + - thêm các đường dẫn thư viện cho singa-kernel, cnmem, cuda và cudnn. + - đi tới các thư viện Dependencies bổ sung để thêm libopenblas.lib, + libglog.lib và libprotobuf.lib + - Đồng thời thêm: singa-kernel.lib, cnmem.lib, cudnn.lib, cuda.lib , + cublas.lib, curand.lib và cudart.lib. + +- tạo singa project + +### Cài đặt Python module + +- Đổi tên \_singa_wrap.so sang \_singa_wrap.pyd trong mục build/python/setup.py +- Copy các tệp tin trong src/proto/python_out sang build/python/singa/proto + +- Không bắt buộc, tạo và kích hoạt virtual environment: + + ```shell + mkdir SingaEnv + virtualenv SingaEnv + SingaEnv\Scripts\activate + ``` + +- tới thư mục build/python và chạy: + + ```shell + python setup.py install + ``` + +- Sử dụng \_singa_wrap.pyd, libglog.dll, libopenblas.dll, cnmem.dll, CUDA + Runtime (e.g. cudart64_91.dll) và cuDNN (e.g. cudnn64_7.dll) bằng cách thêm + chúng vào đường dẫn hoặc copy chúng vào thư mục gói chương trình singa trong + gói python site-packages + +- Xác nhận SINGA đã được cài đặt bằng cách chạy: + + ```shell + python -c "from singa import device; dev = device.create_cuda_gpu()" + ``` + +Tham khảo video hướng dẫn cho mục này tại đây: + +[![youtube video](https://img.youtube.com/vi/YasKVjRtuDs/0.jpg)](https://www.youtube.com/watch?v=YasKVjRtuDs) + +### Run Unit Tests + +- Trong thư mục tests, tạo Visual Studio solution: + + ```shell + cmake -G "Visual Studio 15 2017 Win64" + ``` + +- Mở solution được tạo trong Visual Studio, hoặc thêm project vào singa solution + đã được tạo ở bước 5.2 + +- Thay đổi cài đặt thiết lập Settings tới Release and x64. + +- Tạo glog project. + +- Trong test_singa project: + + - Thêm USE_GLOG; USE_CUDA; USE_CUDNN vào Định Nghĩa Preprocessor. + - Trong Thư viện Dependencies bổ sung, thêm đường dẫn của GLOG_INCLUDE_DIR, + CBLAS_INCLUDE_DIR và Protobuf_INCLUDE_DIR đã được sử dụng tại bước 5.2 ở + trên. Và thêm build, build/include, CUDA và cuDNN bao gồm thư mục. + - Tới Thư viện Dependencies bổ sung và thêm đường dẫn của thư viện openblas, + protobuf và glog. Và thêm đường dẫn thư viện của + build/src/singa_objects.dir/Release, singa-kernel, cnmem, CUDA và cuDNN. + - Tới Thư viện Dependencies bổ sung và thêm libopenblas.lib; libglog.lib; + libprotobuf.lib; cnmem.lib; cudnn.lib; cuda.lib; cublas.lib; curand.lib; + cudart.lib; singa-kernel.lib. Sửa tên của hai thư viện: gtest.lib và + singa_objects.lib. + +* Tạo test_singa project. + +* Sử dụng libglog.dll, libopenblas.dll, cnmem.dll, cudart64_91.dll và + cudnn64_7.dll bằng cách thêm chúng vào đường dẫn hoặc copy chúng vào thư mục + test/release. + +- Tiến hành unit tests bằng cách: + + - Từ dòng lệnh: + + ```shell + test_singa.exe + ``` + + - Từ Visual Studio: + - ấn chuột phải tại test_singa project và chọn 'Set as StartUp Project'. + - Từ mục Debug menu, chọn 'Start Without Debugging' + +Tham khảo video hướng dẫn chạy unit tests tại đây: + +[![youtube video](https://img.youtube.com/vi/YOjwtrvTPn4/0.jpg)](https://www.youtube.com/watch?v=YOjwtrvTPn4) diff --git a/docs-site/website/versioned_docs/version-4.3.0_Viet/installation.md b/docs-site/website/versioned_docs/version-4.3.0_Viet/installation.md new file mode 100644 index 0000000..1e59f6d --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0_Viet/installation.md @@ -0,0 +1,139 @@ +--- +id: version-4.3.0_Viet-installation +title: Cài đặt +original_id: installation +--- + + + +## Sử dụng Pip + +1. Cho CPU + [![Mở trên Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/17RA056Brwk0vBQTFaZ-l9EbqwADO0NA9?usp=sharing) + +```bash +pip install singa -f http://singa.apache.org/docs/next/wheel-cpu.html --trusted-host singa.apache.org +``` + +Bạn có thể cài đặt một phiên bản SINGA cụ thể sử dụng `singa==`, thay +thông tin ``, v.d, `.3.0`. Xem danh sách các phiên bản SINGA đang hoạt +động ở đường dẫn. + +Để cài đặt phiên bản phát triển mới nhất, thay đường dẫn bằng +http://singa.apache.org/docs/next/wheel-cpu-dev.html + +2. GPU với CUDA và cuDNN + [![Mở trên Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1W30IPCqj5fG8ADAQsFqclaCLyIclVcJL?usp=sharing) + +```bash +pip install singa -f http://singa.apache.org/docs/next/wheel-gpu.html --trusted-host singa.apache.org +``` + +Bạn có thể thiết lập phiên bản SINGA và CUDA, nh�2.0+cuda10.2`. Danh sách tổ hợp +phiên bản SINGA với CUDA được cung cấp trong đường dẫn. + +Để cài đặt phiên bản phát triển mới nhất, thay đường dẫn bằng +http://singa.apache.org/docs/next/wheel-gpu-dev.html + +Lưu ý: phiên bản Python của Python environment trong máy của bạn sẽ được sử dụng +để tìm gói wheel tương ứng. Ví dụ, nếu bạn sử dụng Python 3.9, thì gói wheel kết +cấu trong Python 3.9 sẽ được pip chọn để cài đặt. Thực tế, tên của tệp tin wheel +bao gồm phiên bản SINGA, phiên bản CUDA và Python. Vì thế, `pip` biết tệp tin +wheel nào để tải và cài đặt. + +Tham khảo chú thích ở phần đầu của tệp tin `setup.py` về cách tạo các gói wheel +packages. + +Nếu không có lỗi khi chạy + +```shell +$ python -c "from singa import tensor" +``` + +thì bạn đã cài đặt SINGA thành công. + +## Sử dụng Docker + +Cài đặt Docker vào máy chủ local theo +[hướng dẫn](https://docs.docker.com/install/). Thêm người dùng vào +[nhóm docker](https://docs.docker.com/install/linux/linux-postinstall/) để chạy +câu lệnh docker mà không cần dùng `sudo`. + +1. Cho CPU. + +```shell +$ docker run -it apache/singa:X.Y.Z-cpu-ubuntu16.04 /bin/bash +``` + +2. Với GPU. Cài đặt [Nvidia-Docker](https://github.com/NVIDIA/nvidia-docker) sau + khi cài Docker. + +```shell +$ nvidia-docker run -it apache/singa:X.Y.Z-cuda9.0-cudnn7.4.2-ubuntu16.04 /bin/bash +``` + +3. Xem danh sách toàn bộ SINGA Docker images (tags), tại + [trang web docker hub](https://hub.docker.com/r/apache/singa/). Với mỗi + docker image, tag được đặt tên theo + +```shell +version-(cpu|gpu)[-devel] +``` + +| Tag | Mô tả | Ví dụ giá trị | +| ----------- | ------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| `phiên bản` | phiên bản SINGA | '2.0.0-rc0', '2.0.0', '1.2.0' | +| `cpu` | image không thể sử dụng cho GPUs | 'cpu' | +| `gpu` | image có thể sử dụng cho Nvidia GPUs | 'gpu', or 'cudax.x-cudnnx.x' e.g., 'cuda10.0-cudnn7.3' | +| `devel` | chỉ số phát triển | nếu không có, gói SINGA Python package chỉ được cài đặt cho runtime; nếu có, environment cũng được tạo ra, bạn có thể kết cấu lại SINGA từ nguồn tại '/root/singa' | +| `OS` | cho biết phiên bản OS | 'ubuntu16.04', 'ubuntu18.04' | + +## Từ nguồn + +Bạn có thể [tạo và cài đặt SINGA](build.md) từ mã code nguồn sử dụng các công cụ +tạo chương trình hoặc conda-build, trên hệ điều hành máy chủ cục bộ (local host +os) hay trong Docker container. + +## Câu Hỏi Thường Gặp + +- Q: Lỗi khi chạy `from singa import tensor` + + A: Kiểm tra chi tiết lỗi từ + + ```shell + python -c "from singa import _singa_wrap" + # tới thưu mục chứa _singa_wrap.so + ldd path to _singa_wrap.so + python + >> import importlib + >> importlib.import_module('_singa_wrap') + ``` + + Thư mục chứa `_singa_wrap.so` thường ở + `~/miniconda3/lib/python3.10/site-packages/singa`. Thông thường, lỗi này được + gây ra bởi sự không tương thích hoặc thiếu các thư viện dependent libraries, + v.d cuDNN hay protobuf. Cách giải quyết là tạo một virtual environment mới và + cài đặt SINGA trong environment đó, v.d, + + ```shell + conda create -n singa + conda activate singa + conda install -c nusdbsystem -c conda-forge singa-cpu + ``` + +- Q: Khi sử dụng virtual environment, mỗi khi tôi cài SINGA, numpy cũng tự động + bị cài lại. Tuy nhiên, numpy không được sử dụng khi chạy `import numpy` + + A: Lỗi này có thể do biến `PYTHONPATH` environment lẽ ra phải để trống trong + khi bạn sử dụng virtual environment để tránh mâu thuẫn với đường dẫn của + virtual environment. + +- Q: Khi chạy SINGA trên Mac OS X, tôi gặp lỗi "Fatal Python error: + PyThreadState_Get: no current thread Abort trap: 6" + + A: Lỗi này thường xảy ra khi bạn có nhiều phiên bản Python trong hệ thống, + v.d, bản của OS và bản được cài bởi Homebrew. Bản Python dùng cho SINGA phải + giống với bản Python interpreter. Bạn có thể kiểm tra interpreter của mình + bằng `which python` và kiểm tra bản Python dùng cho SINGA qua + `otool -L `. Vấn đề này được giải quyết nếu SINGA được + cài qua conda. diff --git a/docs-site/website/versioned_docs/version-4.3.0_Viet/issue-tracking.md b/docs-site/website/versioned_docs/version-4.3.0_Viet/issue-tracking.md new file mode 100644 index 0000000..1fe895e --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0_Viet/issue-tracking.md @@ -0,0 +1,13 @@ +--- +id: version-4.2.0_Viet-issue-tracking +title: Theo Dõi Vấn Đề +original_id: issue-tracking +--- + + + +SINGA sử dụng [JIRA](https://issues.apache.org/jira/browse/singa) để quản lý các +vấn đề bao gồm lỗi (bugs), các tính năng mới và thảo luận. + +Chúng tôi đang trong quá trình chuyển sang mục +[Github Issues](https://github.com/apache/singa/issues). diff --git a/docs-site/website/versioned_docs/version-4.3.0_Viet/mail-lists.md b/docs-site/website/versioned_docs/version-4.3.0_Viet/mail-lists.md new file mode 100644 index 0000000..505058c --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0_Viet/mail-lists.md @@ -0,0 +1,17 @@ +--- +id: version-4.2.0_Viet-mail-lists +title: Danh sách liên hệ của Dự Án +original_id: mail-lists +--- + + + +Có một danh sách liên hệ được thiết lập cho dự án này. Mỗi danh sách bao gồm +đường dẫn để đăng kí(subscribe), huỷ đăng kí (unsubscribe), và đường dẫn tới kho +lưu trữ. + +| Tên | Loại | Đăng kí | Huỷ Đăng kí | Lưu Trữ | +| ---------- | ------------------------------------ | -------------------------------------------------------------- | -------------------------------------------------------------------- | ----------------------------------------------------------------------------------- | +| Phát triển | | [Đăng kí](mailto:dev-subscribe@singa.incubator.apache.org) | [Huỷ đăng kí](mailto:dev-unsubscribe@singa.incubator.apache.org.) | [mail-archives.apache.org](http://mail-archives.apache.org/mod_mbox/singa-dev/) | +| Đóng góp | | [Đăng kí](mailto:commits-subscribe@singa.incubator.apache.org) | [Huỷ đăng kí](mailto:commits-unsubscribe@singa.incubator.apache.org) | [mail-archives.apache.org](http://mail-archives.apache.org/mod_mbox/singa-commits/) | +| Bảo mật | | private | private | private | diff --git a/docs-site/website/versioned_docs/version-4.3.0_Viet/onnx.md b/docs-site/website/versioned_docs/version-4.3.0_Viet/onnx.md new file mode 100644 index 0000000..3075e3d --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0_Viet/onnx.md @@ -0,0 +1,410 @@ +--- +id: version-4.2.0_Viet-onnx +title: ONNX +original_id: onnx +--- + + + +[ONNX](https://onnx.ai/) là một định dạng đại diện mở dùng trong các model của +machine learning, cho phép nhà phát triển AI sử dụng các models trên các +libraries và công cụ khác nhau. SINGA hỗ trợ tải các models dạng ONNX trong +training và inference, và lưu các models ở dạng ONNX với SINGA APIs (e.g., +[Module](./module)). + +SINGA đã được thử nghiệm với +[phiên bản sau](https://github.com/onnx/onnx/blob/master/docs/Versioning.md) của +ONNX. + +| Phiên bản ONNX | Phiên bản định dạng tệp tin | Opset phiên bản ai.onnx | Opset phiên bản ai.onnx.ml | Opset phiên bản ai.onnx.training | +| -------------- | --------------------------- | ----------------------- | -------------------------- | -------------------------------- | +| 1.6.0 | 6 | 11 | 2 | - | + +## Sử dụng chung + +### Tải một ONNX Model trong SINGA + +Sau khi tải một ONNX model từ disk qua `onnx.load`, bạn chỉ cần cập nhật +batch-size của input sử dụng `tensor.PlaceHolder` sau SINGA v3.0, shape của +internal tensors sẽ tự động được tạo ra. + +Sau đó, bạn định nghĩa một class thừa hưởng từ `sonnx.SONNXModel` và thực hiện +hai phương pháp `forward` cho quá trình forward và `train_one_batch` cho quá +trình training. Sau khi gọi hàm `model.compile`, hàm SONNX sẽ lặp lại và dịch +tất cả các nodes trong phạm vi graph của ONNX model sang các hàm SINGA, tải tất +cả weights đã lưu trữ và tạo ra shape của từng tensor trung gian. + +```python3 +import onnx +from singa import device +from singa import sonnx + +class MyModel(sonnx.SONNXModel): + + def __init__(self, onnx_model): + super(MyModel, self).__init__(onnx_model) + + def forward(self, *x): + y = super(MyModel, self).forward(*x) + # Since SINGA model returns the output as a list, + # if there is only one output, + # you just need to take the first element. + return y[0] + + def train_one_batch(self, x, y): + pass + +model_path = "PATH/To/ONNX/MODEL" +onnx_model = onnx.load(model_path) + +# convert onnx model into SINGA model +dev = device.create_cuda_gpu() +x = tensor.PlaceHolder(INPUT.shape, device=dev) +model = MyModel(onnx_model) +model.compile([x], is_train=False, use_graph=True, sequential=True) +``` + +### Inference với SINGA model + +Sau khi tạo models, bạn có thể tiến hành inference bằng cách gọi hàm +`model.forward`. Đầu vào và đầu ra phải ở dạng phiên bản của SINGA `Tensor`. + +```python3 +x = tensor.Tensor(device=dev, data=INPUT) +y = model.forward(x) +``` + +### Lưu model của SINGA dưới dạng ONNX + +Với hàm tensors đầu vào và đầu ra được tạo ra bởi các hàm của model, bạn có thể +truy nguyên đến tất cả các hàm nội bộ. Bởi vậy, một model SINGA được xác định +bởi tensors đầu vào và đầu ra. Để biến một model SINGA sang dạng ONNX, bạn chỉ +cần cung cấp danh sách tensor đầu vào và đầu ra. + +```python3 +# x is the input tensor, y is the output tensor +sonnx.to_onnx([x], [y]) +``` + +### Training lại với model ONNX + +Để train (hay luyện) một model ONNX sử dụng SINGA, bạn cần thực hiện +`train_one_batch` từ `sonnx.SONNXModel` và đánh dấu `is_train=True` khi gọi hàm +`model.compile`. + +```python3 +from singa import opt +from singa import autograd + +class MyModel(sonnx.SONNXModel): + + def __init__(self, onnx_model): + super(MyModel, self).__init__(onnx_model) + + def forward(self, *x): + y = super(MyModel, self).forward(*x) + return y[0] + + def train_one_batch(self, x, y, dist_option, spars): + out = self.forward(x) + loss = autograd.softmax_cross_entropy(out, y) + if dist_option == 'fp32': + self.optimizer.backward_and_update(loss) + elif dist_option == 'fp16': + 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 + +sgd = opt.SGD(lr=0.005, momentum=0.9, weight_decay=1e-5) +model.set_optimizer(sgd) +model.compile([tx], is_train=True, use_graph=graph, sequential=True) +``` + +### Transfer-learning một model ONNX + +Bạn cũng có thể thêm một vài layers vào phần cuối của ONNX model để làm +transfer-learning. Hàm `last_layers` chấp nhận một số nguyên âm để chỉ layer bị +cắt ra. Ví dụ, `-1` nghĩa là bị cắt ra sau kết quả cuối cùng (không xoá bớt +layer nào) `-2` nghĩa là bị cắt ra sau hai layer cuối cùng. + +```python3 +from singa import opt +from singa import autograd + +class MyModel(sonnx.SONNXModel): + + def __init__(self, onnx_model): + super(MyModel, self).__init__(onnx_model) + self.linear = layer.Linear(1000, 3) + + def forward(self, *x): + # cut off after the last third layer + # and append a linear layer + y = super(MyModel, self).forward(*x, last_layers=-3)[0] + y = self.linear(y) + return y + + def train_one_batch(self, x, y, dist_option, spars): + out = self.forward(x) + loss = autograd.softmax_cross_entropy(out, y) + if dist_option == 'fp32': + self.optimizer.backward_and_update(loss) + elif dist_option == 'fp16': + 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 + +sgd = opt.SGD(lr=0.005, momentum=0.9, weight_decay=1e-5) +model.set_optimizer(sgd) +model.compile([tx], is_train=True, use_graph=graph, sequential=True) +``` + +## ONNX model zoo + +[ONNX Model Zoo](https://github.com/onnx/models) là tổ hợp các models ở dạng +ONNX, đã được train có kết quả tốt nhất, đóng góp bởi cộng đồng thành viên. +SINGA giờ đây đã hỗ trợ một số models CV và NLP. Chúng tôi dự định sẽ sớm hỗ trợ +thêm các models khác. + +### Phân loại hình ảnh (Image Classification) + +Tổ hợp models này có đầu vào là hình ảnh, sau đó phân loại các đối tượng chính +trong hình ảnh thành 1000 mục đối tượng như bàn phím, chuột, bút chì, và các +động vật. + +| Model Class | Tham khảo | Mô tả | Đường dẫn | +| --------------------------------------------------------------------------------------------------- | ------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- | +| [MobileNet](https://github.com/onnx/models/tree/master/vision/classification/mobilenet) | [Sandler et al.](https://arxiv.org/abs/1801.04381) | deep neural network nhỏ, nhẹ phù hợp nhất cho điện thoại và ứng dụng hình ảnh đính kèm.
Top-5 error từ báo cáo - ~10% | [![Mở trên Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1HsixqJMIpKyEPhkbB8jy7NwNEFEAUWAf) | +| [ResNet18](https://github.com/onnx/models/tree/master/vision/classification/resnet) | [He et al.](https://arxiv.org/abs/1512.03385) | Mô hình CNN (lên tới 152 layers). Sử dụng liên kết ngắn gọn để đạt độ chính xác cao hơn khi phân loại hình ảnh.
Top-5 error từ báo cáo - ~3.6% | [![Mở trên Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1u1RYefSsVbiP4I-5wiBKHjsT9L0FxLm9) | +| [VGG16](https://github.com/onnx/models/tree/master/vision/classification/vgg) | [Simonyan et al.](https://arxiv.org/abs/1409.1556) | Mô hình CNN chuyên sâu (lên tới 19 layers). Tương tự như AlexNet nhưng sử dụng nhiều loại filters cỡ kernel nhỏ hơn mang lại độ chính xác cao hơn khi phân loại hình ảnh.
Top-5 từ báo cáo - ~8% | [![Mở trên Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/14kxgRKtbjPCKKsDJVNi3AvTev81Gp_Ds) | +| [ShuffleNet_V2](https://github.com/onnx/models/tree/master/vision/classification/shufflenet) | [Simonyan et al.](https://arxiv.org/pdf/1707.01083.pdf) | Mô hình CNN cực kỳ hiệu quả trong sử dụng tài nguyên, được thiết kế đặc biệt cho các thiết bị di động. Mạng lưới thiết kế hệ mô hình sử dụng số liệu trực tiếp như tốc độ, thay vì các số liệu gián tiếp như FLOP. Top-1 error từ báo cáo - ~30.6% | [![Mở trên Colab](https://colab.research.google.com/drive/19HfRu3YHP_H2z3BcZujVFRp23_J5XsuA?usp=sharing) | + +Chúng tôi cung cấp ví dụ re-training sử dụng VGG và ResNet, vui lòng xem tại +`examples/onnx/training`. + +### Nhận Diện Đối Tượng (Object Detection) + +Các models Object detection nhận diện sự hiện diện của các đối tượng trong một +hình ảnh và phân đoạn ra các khu vực của bức ảnh mà đối tượng được nhận diện. + +| Model Class | Tham khảo | Mô tả | Đường dẫn | +| ----------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- | +| [Tiny YOLOv2](https://github.com/onnx/models/tree/master/vision/object_detection_segmentation/tiny_yolov2) | [Redmon et al.](https://arxiv.org/pdf/1612.08242.pdf) | Mô hình CNN thời gian thực cho Nhận diện đối tượng có thể nhận diện 20 loại đối tượng khác nhau. Phiên bản nhỏ của mô hình phức tạp Yolov2. | [![Mở trên Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/11V4I6cRjIJNUv5ZGsEGwqHuoQEie6b1T) | + +### Phân tích Khuôn Mặt (Face Analysis) + +Các mô hình Nhận Diện Khuôn Mặt xác định và/hoặc nhận diện khuôn mặt người và +các trạng thái cảm xúc trong bức ảnh. + +| Model Class | Tham khảo | Mô tả | Đường dẫn | +| --------------------------------------------------------------------------------------------------------- | -------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- | +| [ArcFace](https://github.com/onnx/models/tree/master/vision/body_analysis/arcface) | [Deng et al.](https://arxiv.org/abs/1801.07698) | Mô hình dựa trên CNN để nhận diện khuôn mặt, học từ các đặc tính khác nhau trên khuôn mặt và tạo ra các embeddings cho hình ảnh khuôn mặt đầu vào. | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1qanaqUKGIDtifdzEzJOHjEj4kYzA9uJC) | +| [Emotion FerPlus](https://github.com/onnx/models/tree/master/vision/body_analysis/emotion_ferplus) | [Barsoum et al.](https://arxiv.org/abs/1608.01041) | Mô hình CNN chuyên sâu nhận diện cảm xúc được train trên các hình ảnh khuôn mặt. | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1XHtBQGRhe58PDi4LGYJzYueWBeWbO23r) | + +### Máy Hiểu (Machine Comprehension) + +Một dạng của mô hình xử lý ngôn ngữ tự nhiên giúp trả lời câu hỏi trên một đoạn +ngôn ngữ cung cấp. + +| Model Class | Tham khảo | Mô tả | Đường dẫn | +| ----------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| [BERT-Squad](https://github.com/onnx/models/tree/master/text/machine_comprehension/bert-squad) | [Devlin et al.](https://arxiv.org/pdf/1810.04805.pdf) | Mô hình này trả lời câu hỏi dựa trên ngữ cảnh của đoạn văn đầu vào. | [![Mở trên Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1kud-lUPjS_u-TkDAzihBTw0Vqr0FjCE-) | +| [RoBERTa](https://github.com/onnx/models/tree/master/text/machine_comprehension/roberta) | [Devlin et al.](https://arxiv.org/pdf/1907.11692.pdf) | Mô hình transformer-based kích thước lớn, dự đoán ngữ nghĩa dựa trên đoạn văn đầu vào. | [![Mở trên Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1F-c4LJSx3Cb2jW6tP7f8nAZDigyLH6iN?usp=sharing) | +| [GPT-2](https://github.com/onnx/models/tree/master/text/machine_comprehension/gpt-2) | [Devlin et al.](https://d4mucfpksywv.cloudfront.net/better-language-models/language_models_are_unsupervised_multitask_learners.pdf) | Mô hình ngôn ngữ transformer-based kích thước lớn, đưa ra một đoạn chữ, rồi dự đoán từ tiếp theo. | [![Mở trên Colab](https://colab.research.google.com/drive/1ZlXLSIMppPch6HgzKRillJiUcWn3PiK7?usp=sharing) | + +## Các toán tử (Operators) được hỗ trợ + +Chúng tôi hỗ trợ các toán tử sau: + +- Acos +- Acosh +- Add +- And +- Asin +- Asinh +- Atan +- Atanh +- AveragePool +- BatchNormalization +- Cast +- Ceil +- Clip +- Concat +- ConstantOfShape +- Conv +- Cos +- Cosh +- Div +- Dropout +- Elu +- Equal +- Erf +- Expand +- Flatten +- Gather +- Gemm +- GlobalAveragePool +- Greater +- HardSigmoid +- Identity +- LeakyRelu +- Less +- Log +- MatMul +- Max +- MaxPool +- Mean +- Min +- Mul +- Neg +- NonZero +- Not +- OneHot +- Or +- Pad +- Pow +- PRelu +- Reciprocal +- ReduceMean +- ReduceSum +- Relu +- Reshape +- ScatterElements +- Selu +- Shape +- Sigmoid +- Sign +- Sin +- Sinh +- Slice +- Softmax +- Softplus +- Softsign +- Split +- Sqrt +- Squeeze +- Sub +- Sum +- Tan +- Tanh +- Tile +- Transpose +- Unsqueeze +- Upsample +- Where +- Xor + +### Các lưu ý đặc biệt cho ONNX backend + +- Conv, MaxPool và AveragePool + + Đầu vào phải có shape 1d`(N*C*H)` và 2d(`N*C*H*W`) trong khi `dilation` phải + là 1. + +- BatchNormalization + + `epsilon` là 1e-05 và không được đổi. + +- Cast + + Chỉ hỗ trợ float32 và int32, các dạng khác phải được cast thành hai dạng này. + +- Squeeze và Unsqueeze + + Nếu gặp lỗi khi dùng `Squeeze` hay `Unsqueeze` giữa `Tensor` và Scalar, vui + lòng báo cho chúng tôi. + +- Empty tensor Empty tensor không được chấp nhận trong SINGA. + +## Thực hiện + +Mã code của SINGA ONNX được đặt trong `python/singa/soonx.py`. Có bốn loại +chính, `SingaFrontend`, `SingaBackend`, `SingaRep` và `SONNXModel`. +`SingaFrontend` qui đổi mô hình SINGA model sang mô hình ONNX model; +`SingaBackend` biến mô hình ONNX model sang đối tượng `SingaRep` giúp lưu trữ +tất cả các toán tử SINGA operators và tensors(tensor trong văn bản này nghĩa là +SINGA `Tensor`); `SingaRep` có thẻ chạy giống như mô hình SINGA model. +`SONNXModel` tạo ra từ `model.Model` xác định thống nhất API cho SINGA. + +### SingaFrontend + +Hàm function đầu vào của `SingaFrontend` là `singa_to_onnx_model` cũng được gọi +là `to_onnx`. `singa_to_onnx_model` tạo ra mô hình ONNX model, và nó cũng tạo ra +một ONNX graph bằng việc sử dụng `singa_to_onnx_graph`. + +`singa_to_onnx_graph` chấp nhận đầu ra của mô hình, và lặp lại đệ quy graph của +mô hình SINGA model từ đầu ra để gom tất cả toán tử tạo thành một hàng. Tensors +đầu vào và trực tiếp, v.d, weights để train, của mô hình SINGA model được chọn +cùng một lúc. Đầu vào được lưu trong `onnx_model.graph.input`; đầu ra được lưu +trong `onnx_model.graph.output`; và weights để train được lưu trong +`onnx_model.graph.initializer`. + +Sau đó toán tử SINGA operator trong hàng được đổi sang từng toán tử ONNX +operators. `_rename_operators` xác định tên toán tử giữa SINGA và ONNX. +`_special_operators` xác định function sử dụng để biến đổi toán tử. + +Thêm vào đó, một vài toán tử trong SINGA có các định nghĩa khác với ONNX, chẳng +hạn như, ONNX coi một vài thuộc tính của toán tử SINGA operators là đầu vào, vì +thế `_unhandled_operators` xác định function nào dùng để xử lý toán tử đặc biệt. + +Do dạng bool được coi là dạng int32 trong SINGA, `_bool_operators` địng nghĩa +toán tử có thể chuyển sang dạng bool. + +### SingaBackend + +Function đầu vào của `SingaBackend` là `prepare` kiểm tra phiên bản nào của mô +hình ONNX model rồi gọi `_onnx_model_to_singa_ops`. + +Chức năng của `_onnx_model_to_singa_ops` là để lấy SINGA tensors và operators. +Các tensors được lưu trong một thư viện, theo tên trong ONNX, và operators được +lưu trong hàng ở dạng `namedtuple('SingaOps', ['node', 'operator'])`. Với mỗi +toán tử operator, `node` là một ví dụ từ OnnxNode được dùng để lưu các thông tin +cơ bản của ONNX node; `operator` là forward function cho toán tử SINGA; + +Bước đầu tiên của `_onnx_model_to_singa_ops` có bốn bước, đầu tiên là gọi +`_parse_graph_params` để lấy tất các các tensors lưu trong `params`. Sau đó gọi +hàm `_parse_graph_inputs_outputs` để lấy tất cả thông tin đầu vào đầu ra lưu +trong `inputs` và `outputs`. Cuối cùng nó lặp lại tất cả các nodes trong ONNX +graph và đẩy sang `_onnx_node_to_singa_op` như SINGA operators hoặc layers và +lưu chúng thành `outputs`. Một vài weights được lưu trong ONNX node gọi là +`Constant`, SONNX có thể xử lý chúng bằng `_onnx_constant_to_np` để lưu trong +`params`. + +Cuối cùng class này trả lại một đối tượng `SingaRep` và lưu trên `params`, +`inputs`, `outputs`, `layers`. + +### SingaRep + +`SingaBackend` lưu tất cả SINGA tensors và operators. `run` chấp nhận đầu vào +của mô hình và chạy từng toán tử SINGA operators một, theo hàng của toán tử. +Người dùng có thể sử dụng `last_layers` để xoá mô hình model sau vài layers cuối +cùng. + +### SONNXModel + +`SONNXModel` được tạo từ `sonnx.SONNXModel` và thực hiện phương pháp `forward` +để cung cấp một API đồng bộ với các mô hình SINGA. diff --git a/docs-site/website/versioned_docs/version-4.3.0_Viet/optimizer.md b/docs-site/website/versioned_docs/version-4.3.0_Viet/optimizer.md new file mode 100644 index 0000000..914eb30 --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0_Viet/optimizer.md @@ -0,0 +1,128 @@ +--- +id: version-4.2.0_Viet-optimizer +title: Optimizer +original_id: optimizer +--- + + + +SINGA hỗ trợ đa dạng các thuật toán tối ưu (optimizers) phổ biến bao gồm +stochastic gradient descent với momentum, Adam, RMSProp, và AdaGrad, etc. Với +mỗi thuật toán tối ưu, SINGA hỗ trợ để sử dụng decay schedular để lên kế hoạch +learning rate áp dụng trong các epochs khác nhau. Các mỗi thuật toán tối ưu và +decay schedulers có trong `singa/opt.py`. + +## Tạo thuật toán tối ưu + +1. SGD với momentum + +```python +# xác định hyperparameter learning rate +lr = 0.001 +# xác định hyperparameter momentum +momentum = 0.9 +# xác định hyperparameter weight decay +weight_decay = 0.0001 + +from singa import opt +sgd = opt.SGD(lr=lr, momentum=momentum, weight_decay=weight_decay) +``` + +2. RMSProp + +```python +# xác định hyperparameter learning rate +lr = 0.001 +# xác định hyperparameter rho +rho = 0.9 +# xác định hyperparameter epsilon +epsilon = 1e-8 +# xác định hyperparameter weight decay +weight_decay = 0.0001 + +from singa import opt +sgd = opt.RMSProp(lr=lr, rho=rho, epsilon=epsilon, weight_decay=weight_decay) +``` + +3. AdaGrad + +```python +# xác định hyperparameter learning rate +lr = 0.001 +# xác định hyperparameter epsilon +epsilon = 1e-8 +# xác định hyperparameter weight decay +weight_decay = 0.0001 + +from singa import opt +sgd = opt.AdaGrad(lr=lr, epsilon=epsilon, weight_decay=weight_decay) +``` + +4. Adam + +```python +# xác định hyperparameter learning rate +lr = 0.001 +# xác định hyperparameter beta 1 +beta_1= 0.9 +# xác định hyperparameter beta 2 +beta_1= 0.999 +# xác định hyperparameter epsilon +epsilon = 1e-8 +# xác định hyperparameter weight decay +weight_decay = 0.0001 + +from singa import opt +sgd = opt.Adam(lr=lr, beta_1=beta_1, beta_2=beta_2, epsilon=epsilon, weight_decay=weight_decay) +``` + +## Tạo Decay Scheduler + +```python +from singa import opt + +# xác định learning rate ban đầu +lr_init = 0.001 +# xác định rate của decay trong decay scheduler +decay_rate = 0.95 +# xác định learning rate schedule có ở dạng staircase shape +staircase=True +# xác định bước decay của decay scheduler (trong ví dụ này lr giảm sau mỗi 2 bước) +decay_steps = 2 + +# tạo decay scheduler, schedule của lr trở thành lr_init * (decay_rate ^ (step // decay_steps) ) +lr = opt.ExponentialDecay(0.1, 2, 0.5, True) +# sử dụng lr để tạo một thuật toán tối ưu +sgd = opt.SGD(lr=lr, momentum=0.9, weight_decay=0.0001) +``` + +## Sử dụng thuật toán tối ưu trong Model API + +Khi tạo mô hình model, cần đính kèm thuật toán tối ưu vào model. + +```python +# tạo CNN sử dụng Model API +model = CNN() + +# khởi tạo thuật toán tối ưu và đính vào model +sgd = opt.SGD(lr=0.005, momentum=0.9, weight_decay=1e-5) +model.set_optimizer(sgd) +``` + +Sau đó, khi gọi hàm model, chạy phương pháp `train_one_batch` để sử dụng thuật +toán tối ưu. + +Do vậy, một ví dụ cho lặp lại loop để tối ưu hoá model là: + +```python +for b in range(num_train_batch): + # tạo mini-batch tiếp theo + x, y = ... + + # Copy dữ liệu vào tensors đầu vào + tx.copy_from_numpy(x) + ty.copy_from_numpy(y) + + # Train với một batch + out, loss = model(tx, ty) +``` diff --git a/docs-site/website/versioned_docs/version-4.3.0_Viet/security.md b/docs-site/website/versioned_docs/version-4.3.0_Viet/security.md new file mode 100644 index 0000000..3cde00f --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0_Viet/security.md @@ -0,0 +1,10 @@ +--- +id: version-4.2.0_Viet-security +title: Bảo Mật +original_id: security +--- + + + +Người dùng có thể báo cáo vấn đề bảo mật tới +[Mail List của Nhóm SINGA Security](mailto:security@singa.apache.org) diff --git a/docs-site/website/versioned_docs/version-4.3.0_Viet/software-stack.md b/docs-site/website/versioned_docs/version-4.3.0_Viet/software-stack.md new file mode 100644 index 0000000..75d1fb1 --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0_Viet/software-stack.md @@ -0,0 +1,146 @@ +--- +id: version-4.2.0_Viet-software-stack +title: Software Stack +original_id: software-stack +--- + + + +Cấu trúc phần mềm của SINGA bao gồm hai cấp độ chính, cấp thấp backend classes +và cấp giao diện Python. Hình 1 mô tả chúng cùng với phần cứng. Cấu tạo backend +cung cấp cấu trúc dữ liệu cơ bản cho các mô hình deep learning, khái quát phần +cứng để kế hoạch và thực hiện các phép tính, trong khi thành phần communication +dùng cho distributed training. Giao diện Python tập hợp cấu trúc dữ liệu CPP và +cung cấp các classes cấp cao bổ sung cho việc train neural network, giúp tiện +lợi hơn khi thực hiện các mô hình neural network phức tạp. + +Sau đây, chúng tôi giới thiệu cấu trúc phần mềm từ dưới lên. + +![Cấu trúc phần mềm SINGA V3](assets/singav3.1-sw.png)
**Hình 1 - Cấu trúc +phần mềm SINGA V3.** + +## Backend cấp thấp + +### Device + +Mỗi trường hợp `Device`, hay thiết bị, được tạo ra từ một thiết bị phần cứng, +v.d GPU hay CPU. `Device` quản lý bộ nhớ của cấu trúc dữ liệu, và lên kế hoạch +hoạt động cho việc thực hiện, v.d, trên CUDA streams hay CPU threads. Dựa trên +phần cứng và ngôn ngữ lập trình của nó, SINGA sử dụng các loại device cụ thể +sau: + +- **CudaGPU** tượng trưng cho cạc Nvidia GPU. Đơn vị sử dụng là CUDA streams. +- **CppCPU** là một CPU thông thường. Đơn vị sử dụng là CPU threads. +- **OpenclGPU** là cạc GPU thông thường cho cả Nvidia và AMD. Đơn vị sử dụng là + CommandQueues. Do OpenCL tương thích với rất nhiều thiết bị phần cứng, như + FPGA và ARM, OpenclGPU có thể phù hợp với các thiết bị phần cứng khác. + +### Tensor + +Hàm `Tensor` class là một array đa chiều, lưu trữ biến models, như hình ảnh đầu +vào và bản đồ đặc tính của convolution layer. Mỗi hàm `Tensor` (v.d một tensor) +được đặt trong một thiết bị, giúp quản lý bộ nhớ của tensor và lên kế hoạch +(phép tính) việc thực hiện với mỗi tensor. Hầu hết phép toán trong machine +learning có thể thể hiện (dày hoặc mỏng) dựa trên nghĩa và việc sử dụng tensor. +Bởi vậy SINGA có thể chạy đa dạng nhiều mô hình, bao gồm deep learning và các mô +hình machine learning truyền thống khác. + +### Hàm Operator + +Có hai dạng hàm operators cho tensors, đại số tuyến tính (linear algebra) như +nhân ma trận (matrix multiplication), và các hàm riêng của neural network như +convolution và pooling. Các hàm đại số tuyến tính được dùng như `Tensor` +functions và được áp dụng riêng rẽ với các thiết bị phần cứng khác nhau. + +- CppMath (tensor_math_cpp.h) thực hiện hoạt động tensor sử dụng Cpp cho CppCPU +- CudaMath (tensor_math_cuda.h) thực hiện hoạt động tensor sử dụng CUDA cho + CudaGPU +- OpenclMath (tensor_math_opencl.h) thực hiện hoạt động tensor sử dụng OpenCL + cho OpenclGPU + +Các toán tử neural network cụ thể cũng được sử dụng riêng rẽ như: + +- GpuConvFoward (convolution.h) thực hiện hàm forward function của convolution + qua CuDNN trên Nvidia GPU. +- CpuConvForward (convolution.h) thực hiện hàm forward function của convolution + qua CPP trên CPU. + +Thông thường, người dùng tạo một hàm `Device` và sử dụng nó để tạo ra các hàm +`Tensor`. Khi gọi chức năng Tensor hoặc dùng trong neural network, việc thực +hiện tương ứng cho thiết bị sử dụng sẽ được gọi. Nói cách khác, việc áp dụng các +toán tử là rõ ràng với người dùng. + +Việc dùng Tensor và Device có thể được áp dụng rộng hơn cho đa dạng thiết bị +phần cứng +sử dụng ngôn ngữ lập trình. Mỗi thiết bị phần cứng mới sẽ được hỗ trợ bằng cách +thêm một Device subclass mới và việc áp dụng tương ứng với các toán tử +operators. + +Tối ưu hoá cho tốc độ và bộ nhớ được thực hiện bởi `Scheduler` và `MemPool` của +`Device`. Ví dụ, `Scheduler` tạo ra một [computational graph](./graph) dựa theo +thư viện chương trình của toán tử operators. Sau đó nó có thể tối ưu lệnh thực +hiện của toán tử trong bộ nhớ chia sẻ và song song. + +### Communicator + +`Communicator` là để hỗ trợ [distributed training](./dist-train). Nó áp dụng +communication protocols sử dụng sockets, MPI và NCCL. Thường người dùng chỉ cần +gọi APIs cấp cao như `put()` và `get()` để gửi và nhận tensors. Tối ưu hoá +Communication cho cấu trúc liên kết, kích cỡ tin nhắn, v.v được thực hiện nội +bộ. + +## Giao diện Python + +Tất cả thành phần backend được thể hiện dạng Python modules thông qua SWIG. Thêm +vào đó, các classes sau được thêm vào để hỗ trợ việc áp dụng cho các networks +phức tạp. + +### Opt + +`Opt` và các lớp con áp dụng phương pháp (như SGD) để cập nhật các giá trị tham +số model sử dụng tham số gradients. Một lớp con [DistOpt](./dist-train) đồng bộ +gradients qua các workers trong distributed training bằng cách gọi phương pháp +từ `Communicator`. + +### Hàm Operator + +Hàm `Operator` gói nhiều functions khác nhau sử dụng toán tử Tensor hoặc neural +network từ backend. Ví dụ, hàm forward function và backward function `ReLU` tạo +ra toán tử `ReLU` operator. + +### Layer + +`Layer` và các lớp con gói các toán tử operators bằng tham số. Ví dụ, +convolution và linear operators có tham số weight và bias parameters. Tham số +được duy trì bởi các lớp `Layer` tương ứng. + +### Autograd + +[Autograd](./autograd) sử dụng +[reverse-mode automatic differentiation](https://rufflewind.com/2016-12-30/reverse-mode-automatic-differentiation) +bằng cách ghi nhớ hoạt động của hàm forward functions của các toán tử rồi tự +động gọi hàm backward functions ở chiều ngược lại. Tất cả các hàm functions có +thể được hỗ trợ bởi `Scheduler` để tạo ra một [computational graph](./graph) +nhằm tối ưu hoá hiệu quả và bộ nhớ. + +### Model + +[Model](./graph) cung cấp giao diện cơ bản để thực hiện các mô hình models mới. +Bạn chỉ cần dùng `Model` và định nghĩa việc thực hiện forward propagation của +model bằng cách tạo và gọi các layers của toán tử. `Model` sẽ thực hiện autograd +và tự động cập nhật tham số thông qua `Opt` trong khi dữ liệu để training được +bơm vào đó. Với `Model` API, SINGA có nhiều lợi thế trong cả lập trình mệnh lệnh +và lập trình khai báo. Người dùng sử dụng một hệ thống sử dụng [Model](./graph) +API theo dạng lập trình mệnh lệnh như PyTorch. Khác với PyTorch tạo lại phép +thực thi operations ở mỗi vòng lặp, SINGA hỗ trợ phép thực thi qua cách tạo một +computational graph hàm súc (khi tính năng này được sử dụng) sau vòng lặp đầu +tiên. Graph tương tự như đã được tạo bởi các thư viện sử dụng lập trình khai +báo, như TensorFlow. Vì thế, SINGA có thể áp dụng các kĩ thuật tối ưu hoá bộ nhớ +và tốc độ qua computational graph. + +### ONNX + +Để hỗ trợ ONNX, SINGA áp dụng một [sonnx](./onnx) module, bao gồm: + +- SingaFrontend để lưu SINGA model ở định dạng onnx. +- SingaBackend để tải model định dạng onnx sang SINGA cho training và inference. diff --git a/docs-site/website/versioned_docs/version-4.3.0_Viet/source-repository.md b/docs-site/website/versioned_docs/version-4.3.0_Viet/source-repository.md new file mode 100644 index 0000000..bdf9aa2 --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0_Viet/source-repository.md @@ -0,0 +1,24 @@ +--- +id: version-4.2.0_Viet-source-repository +title: Source Repository +original_id: source-repository +--- + + + +Dự án này sử dụng [Git](http://git-scm.com/) để quản lý nguồn mã code (source +code). Hướng dẫn sử dụng Git xem tại http://git-scm.com/documentation . + +## Repository + +Các đường dẫn sau tới repository của nguồn code online. + +- https://gitbox.apache.org/repos/asf?p=singa.git + +Bản Github mirror tại + +- https://github.com/apache/singa + +Mã code có thể cloned từ cả hai repo, vd., + + git clone https://github.com/apache/singa.git diff --git a/docs-site/website/versioned_docs/version-4.3.0_Viet/team-list.md b/docs-site/website/versioned_docs/version-4.3.0_Viet/team-list.md new file mode 100644 index 0000000..465c59c --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0_Viet/team-list.md @@ -0,0 +1,60 @@ +--- +id: version-4.2.0_Viet-team-list +title: Danh sách nhân sự SINGA +original_id: team-list +--- + + + +Một dự án thành công là công sức của rất nhiều người tham gia. Một số thành viên +viết code hoặc tài liệu, trong khi những người khác góp công sức quí báu vào thử +nghiệm, đề xuất sửa chữa và gợi ý. + +Cộng đồng SINGA bao gồm các lập trình viên đa phần đến từ NUS, ĐH Chiết Giang, +NetEase, ĐH Osaka, yzBigData, v.v + +## PMC + +| Tên | Email | Tổ Chức | +| ------------- | ----------------------- | --------------------------------------------- | +| Gang Chen | cg@apache.org | Zhejiang University | +| Anh Dinh | dinhtta@apache.org | Singapore University of Technology and Design | +| Ted Dunning | tdunning@apache.org | Apache Software Foundation | +| Jinyang Gao | jinyang@apache.org | DAMO Academy, Alibaba Group | +| Alan Gates | gates@apache.org | Apache Software Foundation | +| Zhaojing Luo | zhaojing@apache.org | National University of Singapore | +| Thejas Nair | thejas@apache.org | Apache Software Foundation | +| Beng Chin Ooi | ooibc@apache.org | National University of Singapore | +| Moaz Reyad | moaz@apache.org | Université Grenoble Alpes | +| Kian-Lee Tan | tankianlee@apache.org | National University of Singapore | +| Sheng Wang | wangsh@apache.org | DAMO Academy, Alibaba Group | +| Wei Wang | wangwei@apache.org | National University of Singapore | +| Zhongle Xie | zhongle@apache.org | National University of Singapore | +| Sai Ho Yeung | chrishkchris@apache.org | National University of Singapore | +| Meihui Zhang | meihuizhang@apache.org | Beijing Institute of Technology | +| Kaiping Zheng | kaiping@apache.org | National University of Singapore | + +## Committers + +| Tên | Email | Tổ Chức | +| ------------ | ---------------------- | --------------------------------------------- | +| Xiangrui Cai | caixr@apache.org | Nankai University | +| Chonho Lee | chonho@apache.org | Osaka University | +| Shicong Lin | shicong@apache.org | National University of Singapore | +| Rulin Xing | rulin@apache.org | Huazhong University of Science and Technology | +| Wanqi Xue | xuewanqi@apache.org | Nanyang Technological University | +| Joddiy Zhang | joddiyzhang@apache.org | National University of Singapore | + +## Contributors + +| Tên | Email | Tổ Chức | +| ------------------ | ---------------------------- | -------------------------------- | +| Haibo Chen | hzchenhaibo@corp.netease.com | NetEase | +| Shicheng Chen | chengsc@comp.nus.edu.sg | National University of Singapore | +| Xin Ji | vincent.j.xin@gmail.com | Visenze, Singapore | +| Anthony K. H. Tung | atung@comp.nus.edu.sg | National University of Singapore | +| Ji Wang | wangji@mzhtechnologies.com | Hangzhou MZH Technologies | +| Yuan Wang | wangyuan@corp.netease.com | NetEase | +| Wenfeng Wu | dcswuw@gmail.com | Freelancer, China | +| Kaiyuan Yang | yangky@comp.nus.edu.sg | National University of Singapore | +| Chang Yao | yaochang2009@gmail.com | Hangzhou MZH Technologies | diff --git a/docs-site/website/versioned_docs/version-4.3.0_Viet/tensor.md b/docs-site/website/versioned_docs/version-4.3.0_Viet/tensor.md new file mode 100644 index 0000000..34bcaac --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0_Viet/tensor.md @@ -0,0 +1,283 @@ +--- +id: version-4.2.0_Viet-tensor +title: Tensor +original_id: tensor +--- + + + +Mỗi thực thể Tensor instance là một array đa chiều được đặt trong một thực thể +Device. Thực thể Tensor lưu các biến và cung cấp phép tính đại số tuyến tính cho +các loại thiết bị phần cứng khác nhau mà không cần người dùng để ý. Lưu ý rằng +người dùng cần đảm bảo các toán hạng tensor được đặt ở cùng một thiết bị ngoại +trừ hàm copy. + +## Cách sử dụng Tensor + +### Tạo Tensor + +```python +>>> import numpy as np +>>> from singa import tensor +>>> tensor.from_numpy( np.asarray([[1, 0, 0], [0, 1, 0]], dtype=np.float32) ) +[[1. 0. 0.] + [0. 1. 0.]] +``` + +### Chuyển sang numpy + +```python +>>> a = np.asarray([[1, 0, 0], [0, 1, 0]], dtype=np.float32) +>>> tensor.from_numpy(a) +[[1. 0. 0.] + [0. 1. 0.]] +>>> tensor.to_numpy(tensor.from_numpy(a)) +array([[1., 0., 0.], + [0., 1., 0.]], dtype=float32) +``` + +### Phương pháp Tensor + +```python +>>> t = tensor.from_numpy(a) +>>> t.transpose([1,0]) +[[1. 0.] + [0. 1.] + [0. 0.]] +``` + +biến đổi `tensor` tới 6 chữ số + +```python +>>> a = tensor.random((2,3,4,5,6,7)) +>>> a.shape +(2, 3, 4, 5, 6, 7) +>>> a.reshape((2,3,4,5,7,6)).transpose((3,2,1,0,4,5)).shape +(5, 4, 3, 2, 7, 6) +``` + +### Phương pháp số học Tensor + +`tensor` được đánh giá trong thời gian thực. + +```python +>>> t + 1 +[[2. 1. 1.] + [1. 2. 1.]] +>>> t / 5 +[[0.2 0. 0. ] + [0. 0.2 0. ]] +``` + +`tensor` tạo số học: + +```python +>>> a +[[1. 2. 3.] + [4. 5. 6.]] +>>> b +[[1. 2. 3.]] +>>> a + b +[[2. 4. 6.] + [5. 7. 9.]] +>>> a * b +[[ 1. 4. 9.] + [ 4. 10. 18.]] +>>> a / b +[[1. 1. 1. ] + [4. 2.5 2. ]] +>>> a/=b # inplace operation +>>> a +[[1. 1. 1. ] + [4. 2.5 2. ]] +``` + +`tensor` broadcasting on matrix multiplication (GEMM) + +```python +>>> from singa import tensor +>>> a = tensor.random((2,2,2,3)) +>>> b = tensor.random((2,3,4)) +>>> tensor.mult(a,b).shape +(2, 2, 2, 4) +``` + +### Hàm lập trình Tensor Functions + +Hàm Functions trong `singa.tensor` tạo ra đối tượng `tensor` mới sau khi áp dụng +phép tính trong hàm function. + +```python +>>> tensor.log(t+1) +[[0.6931472 0. 0. ] + [0. 0.6931472 0. ]] +``` + +### Tensor ở các thiết bị khác nhau + +`tensor` được tạo ra trên máy chủ (CPU) từ ban đầu; nó cũng được tạo ra trên các +thiết bị phần cứng khác nhau bằng cách cụ thể hoá `device`. Một `tensor` có thể +chuyển giữa `device`s qua hàm `to_device()` function. + +```python +>>> from singa import device +>>> x = tensor.Tensor((2, 3), device.create_cuda_gpu()) +>>> x.gaussian(1,1) +>>> x +[[1.531889 1.0128608 0.12691343] + [2.1674204 3.083676 2.7421203 ]] +>>> # move to host +>>> x.to_device(device.get_default_device()) +``` + +### Dùng Tensor để train MLP + +```python + +""" + Đoạn mã trích từ examples/mlp/module.py +""" + +label = get_label() +data = get_data() + +dev = device.create_cuda_gpu_on(0) +sgd = opt.SGD(0.05) + +# định nghĩa tensor cho dữ liệu và nhãn đầu vào +tx = tensor.Tensor((400, 2), dev, tensor.float32) +ty = tensor.Tensor((400,), dev, tensor.int32) +model = MLP(data_size=2, perceptron_size=3, num_classes=2) + +# đính model vào graph +model.set_optimizer(sgd) +model.compile([tx], is_train=True, use_graph=True, sequential=False) +model.train() + +for i in range(1001): + tx.copy_from_numpy(data) + ty.copy_from_numpy(label) + out, loss = model(tx, ty, 'fp32', spars=None) + + if i % 100 == 0: + print("training loss = ", tensor.to_numpy(loss)[0]) +``` + +Đầu ra: + +```bash +$ python3 examples/mlp/module.py +training loss = 0.6158037 +training loss = 0.52852553 +training loss = 0.4571422 +training loss = 0.37274635 +training loss = 0.30146334 +training loss = 0.24906921 +training loss = 0.21128304 +training loss = 0.18390492 +training loss = 0.16362564 +training loss = 0.148164 +training loss = 0.13589878 +``` + +## Áp dụng Tensor + +Mục trước chỉ ra cách sử dụng chung của `Tensor`, việc áp dụng cụ thể được đưa +ra sau đây. Đầu tiên, sẽ giới thiệu việc thiết lập tensors Python và C++. Phần +sau sẽ nói về cách frontend (Python) và backend (C++) kết nối với nhau và cách +để mở rộng chúng. + +### Python Tensor + +`Tensor` của lớp Python, được định nghĩa trong `python/singa/tensor.py`, cung +cấp cách dùng tensor ở tầng cao, để thực hiện việc vận hành deep learning (qua +[autograd](./autograd)), cũng như là quản lý dữ liệu bởi người dùng cuối. + +Hoạt động cơ bản của nó là gói xung quanh các phương pháp C++ tensor, cả phương +pháp số học (như `sum`) và không số học (như `reshape`). Một vài phép số học cao +cấp về sau được giới thiệu và áp dụng sử dụng thuần Python tensor API, như +`tensordot`. Python Tensor APIs có thể sử dụng để thực hiện dễ dàng các phép +tính neural network phức tạp với các phương pháp methods linh hoạt có sẵn. + +### C++ Tensor + +`Tensor` lớp C++, được định nghĩa trong `include/singa/core/tensor.h`, về cơ bản +quản lý bộ nhớ nắm giữ dữ liệu, và cung cấp APIs tầm thấp cho các hàm thực hiện +tensor. Đồng thời nó cũng cung cấp các phương pháp số học đa dạng (như `matmul`) +bằng cách gói các chương trình backends khác nhau (CUDA, BLAS, cuBLAS, v.v.). + +#### Văn bản thực hiện và Khoá Bộ nhớ + +Hai khái niệm hay cấu trúc dữ liệu quan trọng của `Tensor` là việc áp dụng +`device`, và khoá bộ nhớ `Block`. + +Mỗi `Tensor` được lưu theo nghiã đen và quản lý bởi một thiết bị phần cứng, thể +hiện theo nghĩa thực hành (CPU, GPU). Tính toán Tensor được thực hiện trên thiết +bị. + +Dữ liệu Tensor trong hàm `Block`, được định nghĩa trong +`include/singa/core/common.h`. `Block` chứa dữ liệu cơ sở, trong khi tensors +chịu trách nhiệm về lý lịch dữ liệu metadata mô tả tensor, như `shape`, +`strides`. + +#### Tensor Math Backends + +Để tận dụng các thư viện chương trình toán hiệu quả cung cấp bởi backend từ các +thiết bị phần cứng khác nhau, SINGA cung cấp một bộ Tensor functions cho mỗi +backend được hỗ trợ. + +- 'tensor_math_cpp.h' áp dụng vận hành sử dụng Cpp (với CBLAS) cho thiết bị + CppCPU. +- 'tensor_math_cuda.h' áp dụng vận hành sử dụng Cuda (với cuBLAS) cho thiết bị + CudaGPU. +- 'tensor_math_opencl.h' áp dụng vận hành sử dụng OpenCL cho thiết bị OpenclGPU. + +### Trình bày C++ APIs qua Python + +SWIG(http://www.swig.org/) là công cụ có thể tự động qui đổi C++ APIs sang +Python APIs. SINGA sử dụng SWIG để trình bày C++ APIs sang Python. Một vài tệp +tin được tạo bởi SWIG, bao gồm `python/singa/singa_wrap.py`. Các Python mô-đun +(như, `tensor`, `device` và `autograd`) nhập mô-đun để gọi C++ APIs để áp dụng +hàm và lớp Python. + +```python +import tensor + +t = tensor.Tensor(shape=(2, 3)) +``` + +Ví dụ, khi một Python `Tensor` instance được tạo ra ở trên, việc áp dụng +`Tensor` class tạo ra một instance của `Tensor` class định nghĩa trong +`singa_wrap.py`, tương ứng với C++ `Tensor` class. Rõ ràng hơn, `Tensor` class +trong `singa_wrap.py` để chỉ `CTensor` trong `tensor.py`. + +```python +# trong tensor.py +from . import singa_wrap as singa + +CTensor = singa.Tensor +``` + +### Tạo Hàm Tensor Functions mới + +Với nền tảng được mô tả phía trên, mở rộng hàm tensor functions có thể dễ dàng +thực hiện từ dưới lên, Với các phép toán, các bước làm như sau: + +- Khai báo API mới cho `tensor.h` +- Tạo mã code sử dụng tiền tố xác định trước trong `tensor.cc`, lấy + `GenUnaryTensorFn(Abs);` làm ví dụ. +- Khai báo theo mẫu method/function trong `tensor_math.h` +- Thực hiện áp dụng ít nhất cho CPU (`tensor_math_cpp.h`) và + GPU(`tensor_math_cuda.h`) +- Trình API qua SWIG bằng cách thêm nó vào `src/api/core_tensor.i` +- Định nghĩa Python Tensor API trong `tensor.py` bằng cách tự động gọi hàm + function được tạo trong `singa_wrap.py` +- Viết unit tests khi phù hợp + +## Python API + +_đang cập nhật_ + +## CPP API + +_đang cập nhật_ diff --git a/docs-site/website/versioned_docs/version-4.3.0_Viet/time-profiling.md b/docs-site/website/versioned_docs/version-4.3.0_Viet/time-profiling.md new file mode 100644 index 0000000..57446d2 --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0_Viet/time-profiling.md @@ -0,0 +1,164 @@ +--- +id: version-4.2.0_Viet-time-profiling +title: Time Profiling +original_id: time-profiling +--- + + + +SINGA hỗ trợ hồ sơ thời gian (time profilier) của mỗi toán tử được lưu tạm thời +trong graph. Để tận dụng chức năng hồ sơ thời gian, chúng tôi trước tiên gọi +method `device.SetVerbosity` để tạo độ dài cho hồ sơ thời gian, sau đó gọi hàm +`device.PrintTimeProfiling` để in ra kết quả của hồ sơ thời gian. + +## Thiết lập Độ dài cho Hồ sơ thời gian + +Để sử dụng chức năng hồ sơ thời gian, bạn cần tạo độ dài cho nó. Có ba mức độ. +Giá trị ban đầu đặt sẵn `verbosity == 0`, là không áp dụng hồ sơ thời gian. Khi +bạn để `verbosity == 1`, nó sẽ lên hồ sơ thời gian chạy forward và backward. Khi +`verbosity == 2`, nó sẽ lên hồ sơ thời gian cho mỗi buffered operation trong +graph. + +Sau đây là mã code ví dụ để thiết lập chức năng hồ sơ thời gian: + +```python +# tạo thiết bị +from singa import device +dev = device.create_cuda_gpu() +# đặt độ dài +verbosity = 2 +dev.SetVerbosity(verbosity) +# không bắt buộc: bỏ qua 5 vòng lặp đầu tiên khi lên hồ sơ thời gian +dev.SetSkipIteration(5) +``` + +Tiếp theo, sau khi kết thúc training ở cuối mỗi chương trình, chúng ta có thể in +kết quả hồ sơ thời gian bằng cách gọi method `device.PrintTimeProfiling`: + +```python +dev.PrintTimeProfiling() +``` + +## Ví dụ đầu ra cho các độ dài khác nhau + +Có thể chạy +[ví dụ](https://github.com/apache/singa/blob/master/examples/cnn/benchmark.py) +ResNet để xem kết quả với cách đặt độ dài khác nhau: + +1. `verbosity == 1` + +``` +Time Profiling: +Forward Propagation Time : 0.0409127 sec +Backward Propagation Time : 0.114813 sec +``` + +2. `verbosity == 2` + +``` +Time Profiling: +OP_ID0. SetValue : 1.73722e-05 sec +OP_ID1. cudnnConvForward : 0.000612724 sec +OP_ID2. GpuBatchNormForwardTraining : 0.000559449 sec +OP_ID3. ReLU : 0.000375004 sec +OP_ID4. GpuPoolingForward : 0.000240041 sec +OP_ID5. SetValue : 3.4176e-06 sec +OP_ID6. cudnnConvForward : 0.000115619 sec +OP_ID7. GpuBatchNormForwardTraining : 0.000150415 sec +OP_ID8. ReLU : 9.95494e-05 sec +OP_ID9. SetValue : 3.22432e-06 sec +OP_ID10. cudnnConvForward : 0.000648668 sec +OP_ID11. GpuBatchNormForwardTraining : 0.000149793 sec +OP_ID12. ReLU : 9.92118e-05 sec +OP_ID13. SetValue : 3.37728e-06 sec +OP_ID14. cudnnConvForward : 0.000400953 sec +OP_ID15. GpuBatchNormForwardTraining : 0.000572181 sec +OP_ID16. SetValue : 3.21312e-06 sec +OP_ID17. cudnnConvForward : 0.000398698 sec +OP_ID18. GpuBatchNormForwardTraining : 0.00056836 sec +OP_ID19. Add : 0.000542246 sec +OP_ID20. ReLU : 0.000372783 sec +OP_ID21. SetValue : 3.25312e-06 sec +OP_ID22. cudnnConvForward : 0.000260731 sec +OP_ID23. GpuBatchNormForwardTraining : 0.000149041 sec +OP_ID24. ReLU : 9.9072e-05 sec +OP_ID25. SetValue : 3.10592e-06 sec +OP_ID26. cudnnConvForward : 0.000637481 sec +OP_ID27. GpuBatchNormForwardTraining : 0.000152577 sec +OP_ID28. ReLU : 9.90518e-05 sec +OP_ID29. SetValue : 3.28224e-06 sec +OP_ID30. cudnnConvForward : 0.000404586 sec +OP_ID31. GpuBatchNormForwardTraining : 0.000569679 sec +OP_ID32. Add : 0.000542291 sec +OP_ID33. ReLU : 0.00037211 sec +OP_ID34. SetValue : 3.13696e-06 sec +OP_ID35. cudnnConvForward : 0.000261219 sec +OP_ID36. GpuBatchNormForwardTraining : 0.000148281 sec +OP_ID37. ReLU : 9.89299e-05 sec +OP_ID38. SetValue : 3.25216e-06 sec +OP_ID39. cudnnConvForward : 0.000633644 sec +OP_ID40. GpuBatchNormForwardTraining : 0.000150711 sec +OP_ID41. ReLU : 9.84902e-05 sec +OP_ID42. SetValue : 3.18176e-06 sec +OP_ID43. cudnnConvForward : 0.000402752 sec +OP_ID44. GpuBatchNormForwardTraining : 0.000571523 sec +OP_ID45. Add : 0.000542435 sec +OP_ID46. ReLU : 0.000372539 sec +OP_ID47. SetValue : 3.24672e-06 sec +OP_ID48. cudnnConvForward : 0.000493054 sec +OP_ID49. GpuBatchNormForwardTraining : 0.000293142 sec +OP_ID50. ReLU : 0.000190047 sec +OP_ID51. SetValue : 3.14784e-06 sec +OP_ID52. cudnnConvForward : 0.00148837 sec +OP_ID53. GpuBatchNormForwardTraining : 8.34794e-05 sec +OP_ID54. ReLU : 5.23254e-05 sec +OP_ID55. SetValue : 3.40096e-06 sec +OP_ID56. cudnnConvForward : 0.000292971 sec +OP_ID57. GpuBatchNormForwardTraining : 0.00029174 sec +OP_ID58. SetValue : 3.3248e-06 sec +OP_ID59. cudnnConvForward : 0.000590154 sec +OP_ID60. GpuBatchNormForwardTraining : 0.000294149 sec +OP_ID61. Add : 0.000275119 sec +OP_ID62. ReLU : 0.000189268 sec +OP_ID63. SetValue : 3.2704e-06 sec +OP_ID64. cudnnConvForward : 0.000341232 sec +OP_ID65. GpuBatchNormForwardTraining : 8.3304e-05 sec +OP_ID66. ReLU : 5.23667e-05 sec +OP_ID67. SetValue : 3.19936e-06 sec +OP_ID68. cudnnConvForward : 0.000542484 sec +OP_ID69. GpuBatchNormForwardTraining : 8.60537e-05 sec +OP_ID70. ReLU : 5.2479e-05 sec +OP_ID71. SetValue : 3.41824e-06 sec +OP_ID72. cudnnConvForward : 0.000291295 sec +OP_ID73. GpuBatchNormForwardTraining : 0.000292795 sec +OP_ID74. Add : 0.000274438 sec +OP_ID75. ReLU : 0.000189689 sec +OP_ID76. SetValue : 3.21984e-06 sec +OP_ID77. cudnnConvForward : 0.000338776 sec +OP_ID78. GpuBatchNormForwardTraining : 8.484e-05 sec +OP_ID79. ReLU : 5.29408e-05 sec +OP_ID80. SetValue : 3.18208e-06 sec +OP_ID81. cudnnConvForward : 0.000545542 sec +OP_ID82. GpuBatchNormForwardTraining : 8.40976e-05 sec +OP_ID83. ReLU : 5.2256e-05 sec +OP_ID84. SetValue : 3.36256e-06 sec +OP_ID85. cudnnConvForward : 0.000293003 sec +OP_ID86. GpuBatchNormForwardTraining : 0.0002989 sec +OP_ID87. Add : 0.000275041 sec +OP_ID88. ReLU : 0.000189867 sec +OP_ID89. SetValue : 3.1184e-06 sec +OP_ID90. cudnnConvForward : 0.000340417 sec +OP_ID91. GpuBatchNormForwardTraining : 8.39395e-05 sec +OP_ID92. ReLU : 5.26544e-05 sec +OP_ID93. SetValue : 3.2336e-06 sec +OP_ID94. cudnnConvForward : 0.000539787 sec +OP_ID95. GpuBatchNormForwardTraining : 8.2753e-05 sec +OP_ID96. ReLU : 4.86758e-05 sec +OP_ID97. SetValue : 3.24384e-06 sec +OP_ID98. cudnnConvForward : 0.000287108 sec +OP_ID99. GpuBatchNormForwardTraining : 0.000293127 sec +OP_ID100. Add : 0.000269478 sec +. +. +. +``` diff --git a/docs-site/website/versioned_docs/version-4.3.0_Viet/wheel-cpu-dev.md b/docs-site/website/versioned_docs/version-4.3.0_Viet/wheel-cpu-dev.md new file mode 100644 index 0000000..ae7b982 --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0_Viet/wheel-cpu-dev.md @@ -0,0 +1,13 @@ +--- +id: version-4.2.0_Viet-wheel-cpu-dev +title: Dùng cho CPU (phiên bản dev) +original_id: wheel-cpu-dev +--- + + + +## 3.0.0.dev200720 + +- [Python 3.6](https://singa-wheel.s3-ap-southeast-1.amazonaws.com/singa-3.0.0.dev200720-cp36-cp36m-manylinux2014_x86_64.whl) +- [Python 3.7](https://singa-wheel.s3-ap-southeast-1.amazonaws.com/singa-3.0.0.dev200720-cp37-cp37m-manylinux2014_x86_64.whl) +- [Python 3.8](https://singa-wheel.s3-ap-southeast-1.amazonaws.com/singa-3.0.0.dev200720-cp38-cp38-manylinux2014_x86_64.whl) diff --git a/docs-site/website/versioned_docs/version-4.3.0_Viet/wheel-cpu.md b/docs-site/website/versioned_docs/version-4.3.0_Viet/wheel-cpu.md new file mode 100644 index 0000000..ab35685 --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0_Viet/wheel-cpu.md @@ -0,0 +1,19 @@ +--- +id: version-4.2.0_Viet-wheel-cpu +title: Dùng cho CPU +original_id: wheel-cpu +--- + + + +## 3.1.0 + +- [Python 3.6](https://singa-wheel.s3-ap-southeast-1.amazonaws.com/singa-3.1.0-cp36-cp36m-manylinux2014_x86_64.whl) +- [Python 3.7](https://singa-wheel.s3-ap-southeast-1.amazonaws.com/singa-3.1.0-cp37-cp37m-manylinux2014_x86_64.whl) +- [Python 3.8](https://singa-wheel.s3-ap-southeast-1.amazonaws.com/singa-3.1.0-cp38-cp38-manylinux2014_x86_64.whl) + +## 3.0.0 + +- [Python 3.6](https://singa-wheel.s3-ap-southeast-1.amazonaws.com/singa-3.0.0-cp36-cp36m-manylinux2014_x86_64.whl) +- [Python 3.7](https://singa-wheel.s3-ap-southeast-1.amazonaws.com/singa-3.0.0-cp37-cp37m-manylinux2014_x86_64.whl) +- [Python 3.8](https://singa-wheel.s3-ap-southeast-1.amazonaws.com/singa-3.0.0-cp38-cp38-manylinux2014_x86_64.whl) diff --git a/docs-site/website/versioned_docs/version-4.3.0_Viet/wheel-gpu-dev.md b/docs-site/website/versioned_docs/version-4.3.0_Viet/wheel-gpu-dev.md new file mode 100644 index 0000000..a35cae7 --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0_Viet/wheel-gpu-dev.md @@ -0,0 +1,13 @@ +--- +id: version-4.2.0_Viet-wheel-gpu-dev +title: Sử Dụng CUDA (phiên bản dev) +original_id: wheel-gpu-dev +--- + + + +## 3.0.0.dev200720 + +- [CUDA10.2, cuDNN 7.6.5, Python 3.6](https://singa-wheel.s3-ap-southeast-1.amazonaws.com/singa-3.0.0.dev200720%2Bcuda10.2-cp36-cp36m-manylinux2014_x86_64.whl) +- [CUDA10.2, cuDNN 7.6.5, Python 3.7](https://singa-wheel.s3-ap-southeast-1.amazonaws.com/singa-3.0.0.dev200720%2Bcuda10.2-cp37-cp37m-manylinux2014_x86_64.whl) +- [CUDA10.2, cuDNN 7.6.5, Python 3.8](https://singa-wheel.s3-ap-southeast-1.amazonaws.com/singa-3.0.0.dev200720%2Bcuda10.2-cp38-cp38-manylinux2014_x86_64.whl) diff --git a/docs-site/website/versioned_docs/version-4.3.0_Viet/wheel-gpu.md b/docs-site/website/versioned_docs/version-4.3.0_Viet/wheel-gpu.md new file mode 100644 index 0000000..31ca170 --- /dev/null +++ b/docs-site/website/versioned_docs/version-4.3.0_Viet/wheel-gpu.md @@ -0,0 +1,22 @@ +--- +id: version-4.2.0_Viet-wheel-gpu +title: Sử Dụng CUDA +original_id: wheel-gpu +--- + + + +## 3.1.0 + +- [CUDA10.2, cuDNN 7.6.5, Python + 3.6]https://singa-wheel.s3-ap-southeast-1.amazonaws.com/singa-3.1.0%2Bcuda10.2-cp36-cp36m-manylinux2014_x86_64.whl) +- [CUDA10.2, cuDNN 7.6.5, Python + 3.7]https://singa-wheel.s3-ap-southeast-1.amazonaws.com/singa-3.1.0%2Bcuda10.2-cp37-cp37m-manylinux2014_x86_64.whl) +- [CUDA10.2, cuDNN 7.6.5, Python + 3.8]https://singa-wheel.s3-ap-southeast-1.amazonaws.com/singa-3.1.0%2Bcuda10.2-cp38-cp38-manylinux2014_x86_64.whl) + +## 3.0.0 + +- [CUDA10.2, cuDNN 7.6.5, Python 3.6](https://singa-wheel.s3-ap-southeast-1.amazonaws.com/singa-3.0.0%2Bcuda10.2-cp36-cp36m-manylinux2014_x86_64.whl) +- [CUDA10.2, cuDNN 7.6.5, Python 3.7](https://singa-wheel.s3-ap-southeast-1.amazonaws.com/singa-3.0.0%2Bcuda10.2-cp37-cp37m-manylinux2014_x86_64.whl) +- [CUDA10.2, cuDNN 7.6.5, Python 3.8](https://singa-wheel.s3-ap-southeast-1.amazonaws.com/singa-3.0.0%2Bcuda10.2-cp38-cp38-manylinux2014_x86_64.whl) diff --git a/docs-site/website/versioned_sidebars/version-4.3.0_Chinese-sidebars.json b/docs-site/website/versioned_sidebars/version-4.3.0_Chinese-sidebars.json new file mode 100644 index 0000000..cabf295 --- /dev/null +++ b/docs-site/website/versioned_sidebars/version-4.3.0_Chinese-sidebars.json @@ -0,0 +1,44 @@ +{ + "version-4.3.0_Chinese-docs": { + "Getting Started": [ + "version-4.3.0_Chinese-installation", + "version-4.3.0_Chinese-software-stack", + "version-4.3.0_Chinese-examples" + ], + "Guides": [ + "version-4.3.0_Chinese-device", + "version-4.3.0_Chinese-tensor", + "version-4.3.0_Chinese-autograd", + "version-4.3.0_Chinese-optimizer", + "version-4.3.0_Chinese-graph", + "version-4.3.0_Chinese-onnx", + "version-4.3.0_Chinese-dist-train", + "version-4.3.0_Chinese-time-profiling", + "version-4.3.0_Chinese-half-precision" + ], + "Development": [ + "version-4.3.0_Chinese-downloads", + "version-4.3.0_Chinese-build", + "version-4.3.0_Chinese-contribute-code", + "version-4.3.0_Chinese-contribute-docs", + "version-4.3.0_Chinese-how-to-release", + "version-4.3.0_Chinese-git-workflow" + ] + }, + "version-4.3.0_Chinese-community": { + "Community": [ + "version-4.3.0_Chinese-source-repository", + "version-4.3.0_Chinese-mail-lists", + "version-4.3.0_Chinese-issue-tracking", + "version-4.3.0_Chinese-security", + "version-4.3.0_Chinese-team-list", + "version-4.3.0_Chinese-history-singa" + ], + "Wheel Package": [ + "version-4.3.0_Chinese-wheel-cpu", + "version-4.3.0_Chinese-wheel-cpu-dev", + "version-4.3.0_Chinese-wheel-gpu", + "version-4.3.0_Chinese-wheel-gpu-dev" + ] + } +} diff --git a/docs-site/website/versioned_sidebars/version-4.3.0_Viet-sidebars.json b/docs-site/website/versioned_sidebars/version-4.3.0_Viet-sidebars.json new file mode 100644 index 0000000..cad69a1 --- /dev/null +++ b/docs-site/website/versioned_sidebars/version-4.3.0_Viet-sidebars.json @@ -0,0 +1,43 @@ +{ + "version-4.3.0_Viet-docs": { + "Getting Started": [ + "version-4.3.0_Viet-installation", + "version-4.3.0_Viet-software-stack", + "version-4.3.0_Viet-examples" + ], + "Guides": [ + "version-4.3.0_Viet-device", + "version-4.3.0_Viet-tensor", + "version-4.3.0_Viet-autograd", + "version-4.3.0_Viet-optimizer", + "version-4.3.0_Viet-graph", + "version-4.3.0_Viet-onnx", + "version-4.3.0_Viet-dist-train", + "version-4.3.0_Viet-time-profiling" + ], + "Development": [ + "version-4.3.0_Viet-downloads", + "version-4.3.0_Viet-build", + "version-4.3.0_Viet-contribute-code", + "version-4.3.0_Viet-contribute-docs", + "version-4.3.0_Viet-how-to-release", + "version-4.3.0_Viet-git-workflow" + ] + }, + "version-4.3.0_Viet-community": { + "Community": [ + "version-4.3.0_Viet-source-repository", + "version-4.3.0_Viet-mail-lists", + "version-4.3.0_Viet-issue-tracking", + "version-4.3.0_Viet-security", + "version-4.3.0_Viet-team-list", + "version-4.3.0_Viet-history-singa" + ], + "Wheel Package": [ + "version-4.3.0_Viet-wheel-cpu", + "version-4.3.0_Viet-wheel-cpu-dev", + "version-4.3.0_Viet-wheel-gpu", + "version-4.3.0_Viet-wheel-gpu-dev" + ] + } +} diff --git a/docs-site/website/versions.json b/docs-site/website/versions.json index a966191..cab0dd9 100644 --- a/docs-site/website/versions.json +++ b/docs-site/website/versions.json @@ -1,4 +1,5 @@ [ + "4.3.0", "4.2.0", "4.1.0", "4.0.0", @@ -11,6 +12,6 @@ "2.0.0", "4.1.0_Chinese", "4.1.0_Viet", - "4.2.0_Chinese", - "4.2.0_Viet" + "4.3.0_Chinese", + "4.3.0_Viet" ] diff --git a/docs-site/website/versions_otherlang.json b/docs-site/website/versions_otherlang.json index 9eca989..246db21 100644 --- a/docs-site/website/versions_otherlang.json +++ b/docs-site/website/versions_otherlang.json @@ -1,4 +1,4 @@ [ - "4.2.0_Chinese", - "4.2.0_Viet" + "4.3.0_Chinese", + "4.3.0_Viet" ]