TVM为NVIDIA GPU自动调优卷积网络

[TOC]

# TVM为NVIDIA GPU自动调优卷积网络

针对特定设备和工作负载进行自动调优对于获得最佳性能至关重要。这篇教程是关于TVM如何为NVIDIA GPU调优整个卷积网络。

TVM中NVIDIA GPU的算子实现是以模板`template`形式编写的。模板有许多可调旋钮`knobs`(平铺`tile`因子,展开`unroll`等)。我们将调优神经网络中的所有**convolutin**和**depthwise-convolution**算子。调优后,我们生成一个log文件,存储所有需要的算子的最佳旋钮值。当TVM编译器编译这些算子时,它将查询此log文件获得最佳旋钮值。

我们还为一些NVIDIA GPU发布了预先调优的参数。您可以访问[NVIDIA GPU Benchmark](https://github.com/dmlc/tvm/wiki/Benchmark#nvidia-gpu)查看结果。

## 加载依赖库

安装步骤与[TVM Compiler中文教程:使用TVM编写可调模板和使用自动调优器](https://blog.csdn.net/hw5226349/article/details/92019491)中步骤一样,安装相应的软件包即可。

python代码中导入包:

```python
import os
import numpy as np

import tvm
from tvm import autotvm
from tvm import relay
import tvm.relay.testing
from tvm.autotvm.tuner import XGBTuner, GATuner, RandomTuner, GridSearchTuner
from tvm.contrib.util import tempdir
import tvm.contrib.graph_runtime as runtime
```

## 定义网络

首先,我们需要调用**relay前端API**中定义网络。我们可以从`nnvm.testing`加载一些预训练网络。我们也可以从MXNet,ONNX和TensorFlow加载模型。

```python
def get_

Change the default wordpress@ email address

https://www.daretothink.co.uk/change-default-wordpress-email-address/

create certificate, key for flask https server

# create with openSSL
#openssl req -x509 -newkey rsa:4096 -nodes -out cert.pem -keyout key.pem -days 365

# invoke the following in the main
    socketio.run(app, certfile='cert.pim', keyfile='key.pim', port=5000)

Classes

Adding classes to a package
# Define a minimal class with an attribute
class MyClass:
    """A minimal example class
    :param value: value to set as the 'attribute' attribute
    :ivar attribute: contains the contents of the 'value' passed in init
    """
    
    # Method to create a new instance of MyClass
    def __init__(self, value):
        # Define attribute with the contents of the value param
        self.attribute = value

Mongo 常用语句

Mongo 常用语句
# Mongo 常用语句
* 跨表联合查询
```bash
rs.slaveOk()
var cid = db.calls.find({pid:10,_id:{$gte:ObjectId("5c16f18db01391a7585e8684")}}).limit(1000).projection({_id:1}).toArray()
var array = new Array()
for(i=0;i<cid.length;i++){
    array.push(cid[i]["_id"])
}
db.call_voice.find({_id:{$in:array}})
```

MySQL常用语句

MySQL常用语句
# MySQL常用语句
* 重置表自增长计数为1
```bash
ALTER TABLE tablename AUTO_INCREMENT = 1
```  
* 生成批量删除同名前缀表的SQL命令
```bash
SELECT CONCAT( 'drop table ', table_name, ';' ) FROM information_schema.tables WHERE table_name LIKE 'prefix_%';
```

Jupyter Notebook使用技巧

Jupyter Notebook使用技巧
# Jupyter Notebook使用技巧
* 变更Jupyter Notebook的起始路径:打开Jupyter Notebook的快捷方式,替换“起始位置”中的“%USERPROFILE%”字段为新的起始路径

WP usage

https://grafana.namecheapcloud.net/d/sTf0slgWk/kubernetes-pod?var-namespace=default&var-pod=deployment-24611-5cc5866fb-5tgx7

k top pod $pod

データベースの管理

--log2テーブルからlog3テーブルを作成する。
--テーブル定義のみコピー参照して作成したい場合はwhere id isnullなどにして出力を0件にする
create table log3
  as select * from log2

--テーブル一覧を取得
show tables from igoinfo

--テーブル製薬
name varchar(20) not null

price default 99

id int(11) not null primary key

Primary key (id, name)

id int(11) not null primary key auto_increment

--重複を許可しない
mail varchaa(100) not null unique

nodejs experimental-report

node --experimental-report --report-uncaught-exception   --report-on-signal --report-signal=SIGQUIT  --report-on-fatalerror --report-filename=./report.json   --report-directory=/home/kaveh/Desktop  app.js

RabbitMQ服务搭建

RabbitMQ服务搭建
# RabbitMQ服务搭建
1. 拉取镜像
```bash
docker pull rabbitmq:management
```
2. 运行容器
```bash
docker run -d --name rabbitmq --publish 5671:5671 --publish 5672:5672 --publish 4369:4369 --publish 25672:25672 --publish 15671:15671 --publish 15672:15672 rabbitmq:management
```
3. 登陆RabbitMQ管理页面:http://host_ip:15672 。用户名和密码均为guest。

Kafka服务搭建

Kafka服务搭建
# Kafka服务搭建
1. 下载源码
```bash
mkdir -p /mydata/kafka-docker/
git clone https://github.com/wurstmeister/kafka-docker
```
2. 修改源码中提供的Dockerfile: 
    * 更新apk 3.9源以提升apk命令执行速度
    * 将需glibc下载到静态文件服务器,然后更新下载地址从静态服务器下载,避免github由于DNS污染导致无法下载到glibc文件
```bash
RUN echo http://mirrors.ustc.edu.cn/alpine/v3.9/main > /etc/apk/repositories \
 && echo http://mirrors.ustc.edu.cn/alpine/v3.9/community >> /etc/apk/repositories \
 && apk add --no-cache bash curl jq docker \
 && chmod a+x /tmp/*.sh \
 && mv /tmp/start-kafka.sh /tmp/broker-list.sh /tmp/create-topics.sh /tmp/versions.sh /usr/bin \
 && sync && /tmp/download-kafka.sh \
 && tar xfz /tmp/kafka_${SCALA_VERSION}-${KAFKA_VERSION}.tgz -C /opt \
 && rm /tmp/kafka_${SCALA_VERSION}-${KAFKA_VERSION}.tgz \
 && ln -s /opt/kafka_${SCALA_VERSION}-${KAFKA_VERSION} /opt/kafka \
 && rm /tmp/* \
 && wget http://xxx.xxx.xxx.xxx:xxxx/mydata/glibc-${GLIBC_VERSION}.apk \
 && apk add --no-cache --allow-untrusted glibc-${GLIBC_VERSION}.apk \
 && rm glibc-${GLIBC_VERS

Java object state equals

Java 基于对象内容的判等
@Override
public boolean equals(Object otherObject) {
  if (this == otherObject) return true;
  if (otherObject == null) return false;
  
  // not allow subclass
  if (getClass() != otherObject.getClass()) return false;
  // allow subclass
  if (!(otherObject instanceof ClassName)) return false;
  
  ClassName other = (ClassName) otherObject;
  return ...;
}

// 修改equals后要对应的修改hashCode
@Override
public int hashCode() {
  return Object.hash(...);
}

extenral module helper

import os
import subprocess
import platform

def Check_dep(debug=False):
	'''
		Check for dependencies and project path.
		
		This method checks to see if the path for the project is included in sys.path.
		This will ensure that the python modules used in the project will 
		be respected by the Touch.
		                
		Notes
		---------------
		'self' does not need to be included in the Args section.
		
		Args
		---------------
		debug (bool):
		> a bool to allow for us to print out the content of sys.path
								
		Returns
		---------------
		None
	'''

	# our path for all non-standard python modules
	dep_path 		= '{}/dep/python/'.format(project.folder)

	# if our path is already present we can skip this step
	if dep_path in sys.path:
		pass

	# insert the python path into our sys.path
	else:
		sys.path.insert(0, dep_path)

	# print each path in sys.path if debug is true:
	if debug:

		for each in sys.path:
			print(each)
	else:
		pass

docker-compose安装

docker-compose安装
# docker-compose安装  
1. 安装pip
```bash
yum update curl
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py
pip install pip
```  
2. 安装docker-compose(网速不佳时可以手动下载docker-compose文件存放到/usr/local/bin/下)
```bash
curl -L https://github.com/docker/compose/releases/download/1.21.2/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
docker-compose --version
```

python, Flask, socketio

from flask_socketio import SocketIO, emit, send

app = Flask(__name__)
app.config['SECRET_KEY'] = 'mysecret'
socketio = SocketIO(app)

# getting the message that is sent from the js
@socketio.on('backup')
def handleMessage(msg):
    print('Message: ' + msg)
    send