5 Star 20 Fork 4

Gitee 极速下载 / jittor

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/Jittor/Jittor
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

Jittor: 即时编译深度学习框架

Jittor Logo

快速开始 | 安装 | 教程 | English

Jittor 是一个基于即时编译和元算子的高性能深度学习框架,整个框架在即时编译的同时,还集成了强大的Op编译器和调优器,为您的模型生成定制化的高性能代码。Jittor还包含了丰富的高性能模型库,涵盖范围包括:图像识别,检测,分割,生成,可微渲染,几何学习,强化学习等等。

Jittor前端语言为Python。前端使用了模块化和动态图执行的设计,这是目前最主流的深度学习框架接口设计。后端则使用高性能语言编写,如CUDA,C++。

相关链接:

下面的代码演示了如何一步一步使用Python代码,从头对一个双层神经网络建模。

import jittor as jt
from jittor import Module
from jittor import nn
import numpy as np

class Model(Module):
    def __init__(self):
        self.layer1 = nn.Linear(1, 10)
        self.relu = nn.Relu() 
        self.layer2 = nn.Linear(10, 1)
    def execute (self,x) :
        x = self.layer1(x)
        x = self.relu(x)
        x = self.layer2(x)
        return x

def get_data(n): # generate random data for training test.
    for i in range(n):
        x = np.random.rand(batch_size, 1)
        y = x*x
        yield jt.float32(x), jt.float32(y)


learning_rate = 0.1
batch_size = 50
n = 1000

model = Model()
optim = nn.SGD(model.parameters(), learning_rate)

for i,(x,y) in enumerate(get_data(n)):
    pred_y = model(x)
    dy = pred_y - y
    loss = dy * dy
    loss_mean = loss.mean()
    optim.step(loss_mean)
    print(f"step {i}, loss = {loss_mean.data.sum()}")

大纲

快速开始

我们提供了一些jupyter notebooks来帮助您快速入门Jittor。

安装

Jittor框架对环境要求如下:

OS CPU Python Compiler (Optional) GPU platform
Linux
(Ubuntu, CentOS, Arch,
UOS, KylinOS, ...)
x86
x86_64
ARM
loongson
>= 3.7 g++ >=5.4 Nvidia CUDA >= 10.0, cuDNN
or AMD ROCm >= 4.0
or Hygon DCU DTK >= 22.04
macOS
(>= 10.14 Mojave)
intel
Apple Silicon
>= 3.7 clang >= 8.0 -
Windows 10 & 11 x86_64 >= 3.8 - Nvidia CUDA >= 10.2 cuDNN

Jittor 提供了三种安装方法:pip、docker和手动安装:

Pip 安装

下面将展示Ubuntu的安装命令,如果您在使用其他Linux操作系统(如CentOS), 请安装好依赖(Python>=3.7, g++>=5.4)或者使用docker安装, 如果您已经装好编译器和对应版本的Python,我们强烈推荐您使用这种方法 (如果无法访问github, 可以通过Jittor主页下载):

sudo apt install python3.7-dev libomp-dev
python3.7 -m pip install jittor
# or install from github(latest version)
# python3.7 -m pip install git+https://github.com/Jittor/jittor.git
python3.7 -m jittor.test.test_example

如果测试运行通过,恭喜你已经安装完成. jittor会自动在路径中寻找合适的编译器, 如果您希望手动指定编译器, 请使用环境变量 cc_pathnvcc_path(可选).

macOS 安装

macOS 请使用 homebrew 安装额外的依赖。

brew install libomp

之后您可以通过 pip 安装 jittor,并测试是否可以成功运行。

python3.7 -m pip install jittor
python3.7 -m jittor.test.test_example

目前在 macOS 中,jittor 只支持 CPU 计算。

Windows安装

Windows 请准备好Python>=3.8,安装方法如下(conda安装需要额外命令):

Windows user please prepare Python>=3.8, install instructions are list below(conda needs extra instructions):

# check your python version(>=3.8)
python --version
python -m pip install jittor
# if conda is used
conda install pywin32

Windows 下,jittor会自动检测显卡并安装对应的 CUDA, 请确保您的NVIDIA驱动支持CUDA 10.2 以上,您还可以使用如下命令手动为Jittor安装CUDA:

python -m jittor_utils.install_cuda

Docker 安装

我们提供了Docker安装方式,免去您配置环境,Docker安装方法如下:

# CPU only(Linux)
docker run -it --network host jittor/jittor
# CPU and CUDA(Linux)
docker run -it --network host --gpus all jittor/jittor-cuda
# CPU only(Mac and Windows)
docker run -it -p 8888:8888 jittor/jittor

关于Docker安装的详细教程,可以参考Windows/Mac/Linux通过Docker安装计图

手动安装

我们将逐步演示如何在Ubuntu 16.04中安装Jittor,其他Linux发行版可能可以使用类似的命令。

步骤一:选择您的后端编译器

# g++
sudo apt install g++ build-essential libomp-dev

# OR clang++-8
wget -O - https://raw.githubusercontent.com/Jittor/jittor/master/script/install_llvm.sh > /tmp/llvm.sh
bash /tmp/llvm.sh 8

步骤二:安装Python和python-dev

Jittor需要python的版本>=3.7。

sudo apt install python3.7 python3.7-dev

步骤三:运行Jittor

整个框架是即时编译的。 让我们通过pip安装jittor

git clone https://github.com/Jittor/jittor.git
sudo pip3.7 install ./jittor
export cc_path="clang++-8"
# if other compiler is used, change cc_path
# export cc_path="g++"
# export cc_path="icc"

# run a simple test
python3.7 -m jittor.test.test_example

如果通过了测试,那么您的Jittor已经准备就绪。

可选步骤四:启用CUDA

在Jittor中使用CUDA非常简单,只需设置环境值nvcc_path

# replace this var with your nvcc location 
export nvcc_path="/usr/local/cuda/bin/nvcc" 
# run a simple cuda test
python3.7 -m jittor.test.test_cuda 

如果测试通过,则可以通过设置use_cuda标识符在Jittor中启用CUDA。

import jittor as jt
jt.flags.use_cuda = 1

可选步骤五:测试训练Resnet18

要检查Jittor的完整性,您可以运行Resnet18训练测试。需要注意的是,这个测试需要6G显存。

python3.7 -m jittor.test.test_resnet

如果这些测试失败,请为我们报告错误,我们十分欢迎您为Jittor做出贡献^ _ ^

教程

在教程部分,我们将简要解释Jittor的基本概念。

要使用Jittor训练模型,您需要了解两个主要概念:

  • Var:Jittor的基本数据类型
  • Operations:Jittor的算子与numpy类似

数据类型

首先,让我们开始使用Var。Var是jittor的基本数据类型,为了运算更加高效Jittor中的计算过程是异步的。 如果要访问数据,可以使用Var.data进行同步数据访问。

import jittor as jt
a = jt.float32([1,2,3])
print (a)
print (a.data)
# Output: float32[3,]
# Output: [ 1. 2. 3.]

此外我们可以给变量起一个名字。

a.name('a')
print(a.name())
# Output: a

数据运算

Jittor的算子与numpy类似。 让我们尝试一些运算, 我们通过Opjt.float32创建Var ab,并将它们相加。 输出这些变量相关信息,可以看出它们具有相同的形状和类型。

import jittor as jt
a = jt.float32([1,2,3])
b = jt.float32([4,5,6])
c = a*b
print(a,b,c)
print(type(a), type(b), type(c))
# Output: float32[3,] float32[3,] float32[3,]
# Output: <class 'jittor_core.Var'> <class 'jittor_core.Var'> <class 'jittor_core.Var'>

除此之外,我们使用的所有算子jt.xxx(Var,...)都具有别名Var.xxx(...)。 例如:

c.max() # alias of jt.max(c)
c.add(a) # alias of jt.add(c, a)
c.min(keepdims=True) # alias of jt.min(c, keepdims=True)

如果您想知道Jittor支持的所有运算,可以运行help(jt.ops)。 您在jt.ops.xxx中找到的所有运算都可以通过别名jt.xxx

help(jt.ops)
# Output:
#   abs(x: core.Var) -> core.Var
#   add(x: core.Var, y: core.Var) -> core.Var
#   array(data: array) -> core.Var
#   binary(x: core.Var, y: core.Var, op: str) -> core.Var
#   ......

更多教程

如果您想进一步了解Jittor,请查看以下notebooks:

这些notebooks可以通过python3.7 -m jittor.notebook在您自己的计算机中运行。

贡献

Jittor还很年轻。 它可能存在错误和问题。 请在我们的错误跟踪系统中报告它们。 我们欢迎您为Jittor做出贡献。 此外,如果您对Jittor有任何想法,请告诉我们。

您可以用以下方式帮助Jittor:

  • 在论文中引用 Jittor
  • 向身边的好朋友推荐 Jittor
  • 贡献代码
  • 贡献教程和文档
  • 提出issue
  • 回答 jittor 相关问题
  • 点亮小星星
  • 持续关注 jittor
  • ……

联系我们

官方主页: http://cg.cs.tsinghua.edu.cn/jittor/

电子邮件:jittor@qq.com

提出issue:https://github.com/Jittor/jittor/issues

QQ 群:761222083

团队

Jittor目前由清华大学计算机图形学组维护。 如果您也对Jittor感兴趣并希望对其进行改进,请加入我们!

引用

@article{hu2020jittor,
  title={Jittor: a novel deep learning framework with meta-operators and unified graph execution},
  author={Hu, Shi-Min and Liang, Dun and Yang, Guo-Ye and Yang, Guo-Wei and Zhou, Wen-Yang},
  journal={Science China Information Sciences},
  volume={63},
  number={222103},
  pages={1--21},
  year={2020}
}

版权声明

如LICENSE.txt文件中所示,Jittor使用Apache 2.0版权协议。

Copyright (c) 2023 Jittor. All Rights Reserved Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright (c) 2023 Jittor. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

计图(Jittor)由清华大学团队研发,是一个完全基于动态编译(Just-in-time),内部使用创新的元算子和统一计算图的深度学习框架 展开 收起
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
C/C++
1
https://gitee.com/mirrors/jittor.git
git@gitee.com:mirrors/jittor.git
mirrors
jittor
jittor
master

搜索帮助