Skip to content

Instantly share code, notes, and snippets.

View ZJUGuoShuai's full-sized avatar
🇨🇳

Guo Shuai ZJUGuoShuai

🇨🇳
  • ByteDance
  • Hangzhou, China
  • 11:28 (UTC +08:00)
View GitHub Profile
@ZJUGuoShuai
ZJUGuoShuai / gemm_by_con2d.py
Created January 2, 2025 07:52
GEMM_by_Conv2D
import torch
import torch.nn.functional as F
def matrix_mul_conv2d(A, B):
"""
A: [M, K]
B: [K, N]
返回: [M, N]
"""
# 1. 重塑输入矩阵 A
@ZJUGuoShuai
ZJUGuoShuai / ln-from-bn.md
Last active February 15, 2024 10:46
使用 Batch Normalization 实现 Layer Normalization

使用 Batch Normalization 实现 Layer Normalization

Batch Normalization 和 Layer Normalization 是深度学习中常用的两种归一化方法,都可以用以下公式表示:

$$ y = \frac{x - \mathrm{E}[x]}{ \sqrt{\mathrm{Var}[x] + \epsilon}} * \gamma + \beta $$

其主要区别就在于 $\mathrm{E}[x]$$\mathrm{Var}[x]$ 是怎么计算的。

@ZJUGuoShuai
ZJUGuoShuai / call-py-from-cpp.cpp
Created December 17, 2023 14:13
Call Python from C++
/**
* @file main.cpp
* @brief 演示如何在 C++ 中将数据传递给 Python,然后在 Python 中用 NumPy 进行计算,最后将结果返回给
* C++。
* @author Guo Shuai ([email protected])
*/
#include <pybind11/pybind11.h>
#include <pybind11/embed.h>
#include <pybind11/numpy.h>
@ZJUGuoShuai
ZJUGuoShuai / opencv-inpaint.ipynb
Last active December 6, 2023 08:52
演示如何使用 OpenCV 图像修复功能填补缺失值
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@ZJUGuoShuai
ZJUGuoShuai / format-from-dali.md
Created December 3, 2023 09:32
一些用于格式化字符串的工具函数(来自 NVIDIA DALI)

使用 make_string()make_string_delim() 将对象格式化为字符串(加分隔符)再拼接起来,类似 Python 里的 str.join()

/**
 * @brief Prints args to a string, without any delimiter
 */
template <typename... Args>
std::string make_string(const Args &... args) {
  std::stringstream ss;
  print(ss, args...);
 return ss.str();
@ZJUGuoShuai
ZJUGuoShuai / zlib-demo.c
Created November 26, 2023 18:03
zlib 极简示例:压缩 100 个 float
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <zlib.h>
#define BUFFER_SIZE 100
int main() {
float buffer[BUFFER_SIZE]; // Assuming you have a buffer of 100 floats
// Fill the buffer with some data (for demonstration purposes)
@ZJUGuoShuai
ZJUGuoShuai / HumanReadable.hpp
Created November 26, 2023 10:00
将字节数打印为人类易读的格式(KB/MB/GB)
struct HumanReadable
{
std::uintmax_t size{};
private:
friend std::ostream& operator<<(std::ostream& os, HumanReadable hr)
{
int o{};
double mantissa = hr.size;
for (; mantissa >= 1024.; mantissa /= 1024., ++o);
@ZJUGuoShuai
ZJUGuoShuai / LRUCache.hpp
Last active February 29, 2024 06:59
A simple LRU Cache in C++ STL.
#include <list>
#include <unordered_map>
#include <utility>
using namespace std;
class LRUCache {
size_t capacity;
list<pair<int, int>> dlist;
@ZJUGuoShuai
ZJUGuoShuai / conv1d-bmm.py
Created October 30, 2023 07:00
批次矩阵乘法 等价于一种特殊的 Conv1D
"""
表明:批次矩阵乘法 等价于一种特殊的 Conv1D
"""
import torch
N = 2
Cin = 3
Lin = 4
Cout = 5
@ZJUGuoShuai
ZJUGuoShuai / fuck-matplotlib.py
Created September 26, 2023 13:29
FUCK MATPLOTLIB CHINESE DISPLAY
""" 如何让 Matplotlib 正确显示中文 """
""" 第一步:通过字体路径创建一个字体属性对象 """
from matplotlib.font_manager import FontProperties
my_font = FontProperties(fname='/Path/To/Your/Font.ttf')
""" 第二步:在所有需要显示中文的绘图命令中增加 fontproperties=my_font 或 prop=my_font 参数 """
plt.xlabel('线程池线程数', fontproperties=my_font)