Auto Byte

Science AI

# OpenAI发布「块稀疏」GPU内核：实现文本情感分析与图像生成建模当前最优水平

• GitHub 地址：https://github.com/openai/blocksparse
• 论文地址：https://s3-us-west-2.amazonaws.com/openai-assets/blocksparse/blocksparsepaper.pdf

### 使用稀疏内核

1. from blocksparse.matmul import BlocksparseMatMul

2. import tensorflow as tf

3. import numpy as np

4. hidden_size = 4096

5. block_size = 32

6. minibatch_size = 64

7. # Create a (random) sparsity pattern

8. sparsity = np.random.randint(2, size=(hidden_size//block_size,hidden_size//block_size))

9. # Initialize the sparse matrix multiplication object

10. bsmm = BlocksparseMatMul(sparsity, block_size=block_size)

11. # Input to graph

12. x = tf.placeholder(tf.float32, shape=[None, hidden_size])

13. # Initialize block-sparse weights

14. w = tf.get_variable("w", bsmm.w_shape, dtype=tf.float32)

15. # Block-sparse matrix multiplication

16. y = bsmm(x, w)

17. # Run

18. sess = tf.InteractiveSession()

19. sess.run(tf.global_variables_initializer())

20. result = sess.run([y], feed_dict = {x: np.ones((minibatch_size,hidden_size), dtype='float32')})

21. print(result)