Skip to content

Commit

Permalink
add more operators
Browse files Browse the repository at this point in the history
  • Loading branch information
BigNoobWasTaken committed Jan 25, 2024
1 parent c6b7cae commit 341cb40
Show file tree
Hide file tree
Showing 15 changed files with 472 additions and 150 deletions.
15 changes: 12 additions & 3 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
{
"version": "2.0.0",
"options": {
"cwd": "${workspaceFolder}/build"
},
"tasks": [
{
"label": "cmake",
"options": {
"cwd": "${workspaceFolder}/build"
},
"command": "cmake",
"args": [
".."
]
},
{
"label": "cpack",
"options": {
"cwd": "${workspaceFolder}/build"
},
"command": "cpack",
"args": [
"-C",
Expand All @@ -21,13 +24,19 @@
},
{
"label": "make-install",
"options": {
"cwd": "${workspaceFolder}/build"
},
"command": "make",
"args": [
"install"
]
},
{
"label": "make-test",
"options": {
"cwd": "${workspaceFolder}/build"
},
"command": "make",
"args": [
"test"
Expand Down
3 changes: 3 additions & 0 deletions docs/1-bit/calculating-loss.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
Before you read this, you need to read:<br>
[operator](operator.md)

## Calculating loss

Normally, you are calculating loss by:
Expand Down
15 changes: 11 additions & 4 deletions docs/1-bit/derivative.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
Before read this, you need to read:<br>
Before you read this, you need to read:<br>
[operator](operator.md)

## Derivative rules

Here are some derivative function that only using one bit and bitwise operator.
Before we read the derivative rule tables, we need to know:
- **c** : constant
- **x** : variable
- **f**, **g** : function
- **** : derivative of


Here are some derivative functions that only using one bit and bitwise operators.

| Common Functions | Function | Derivative |
|:-|:-:|:-:|
Expand All @@ -12,12 +19,12 @@ Here are some derivative function that only using one bit and bitwise operator.
| Square | x & x | x ^ x |
| NOT | ~ x | 1 |

Here are some derivative rules that only using one bit and bitwise operator.
Here are some derivative rules that only using one bit and bitwise operators.

| Rules | Function | Derivative |
|:-|:-:|:-:|
| AND by constant | c & f(x) | c & f’(x) |
| AND rule | f(x) & g(x) | f’(x) & g’(x) |
| XOR rule | f(x) ^ g(x) | (f’(x) & g(x)) ^ (f(x) & g’(x)) |
| NOT rule | ~ f(x) | f’(x) |
| Chain Rule | f(g(x))| f’(g(x)) ^ g’(x) |
| Chain rule | f(g(x)) | f’(g(x)) & g’(x) |
17 changes: 17 additions & 0 deletions docs/1-bit/linear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Before you read this, you need to read:<br>
[operator](operator.md)

## Linear

In normal, we are calulating the linear by using this:

```
y = ax + b
y = (a * x) + b
```

But in 1-bit, we need faster, so we try to make it to:

```
y = (a & x) ^ b
```
File renamed without changes.
2 changes: 1 addition & 1 deletion src/binary_tensor/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ set(CMAKE_CUDA_ARCHITECTURES 52 75 89)
set(CMAKE_CUDA_SEPARABLE_COMPILATION TRUE)
set(CMAKE_CUDA_FLAGS ${CMAKE_CUDA_FLAGS} "--default-stream per-thread")

file(GLOB BinaryTensor_src "dtype/*.cu" "dtype/*.cc" "*.cc" "*.cu")
file(GLOB BinaryTensor_src "dtype/*.cu" "*.cc" "*.cu")

# file(MAKE_DIRECTORY "include/tensor_array/core")

Expand Down
49 changes: 0 additions & 49 deletions src/binary_tensor/core/dtype/uint1_t_x8.cc

This file was deleted.

138 changes: 70 additions & 68 deletions src/binary_tensor/core/dtype/uint1_t_x8.cu
Original file line number Diff line number Diff line change
Expand Up @@ -34,77 +34,79 @@ __host__ __device__ uint1_t_x8::operator unsigned char() const

__device__ uint1_t_x8 atomicAdd(uint1_t_x8*, uint1_t_x8);

__host__ __device__ uint1_t_x8 operator&(const uint1_t_x8& a, const uint1_t_x8& b)
{
return static_cast<const unsigned char&>(a) & static_cast<const unsigned char&>(b);
}

__host__ __device__ uint1_t_x8 operator|(const uint1_t_x8& a, const uint1_t_x8& b)
{
return static_cast<const unsigned char&>(a) | static_cast<const unsigned char&>(b);
}

__host__ __device__ uint1_t_x8 operator^(const uint1_t_x8& a, const uint1_t_x8& b)
{
return static_cast<const unsigned char&>(a) ^ static_cast<const unsigned char&>(b);
}

__host__ __device__ uint1_t_x8 operator~(const uint1_t_x8& values)
{
return ~static_cast<const unsigned char&>(values);
}

__host__ __device__ uint1_t_x8 operator+(const uint1_t_x8& a, const uint1_t_x8& b)
{
return a ^ b;
}

__host__ __device__ uint1_t_x8 operator-(const uint1_t_x8& a, const uint1_t_x8& b)
{
return a ^ b;
}

__host__ __device__ uint1_t_x8 operator*(const uint1_t_x8& a, const uint1_t_x8& b)
{
return a & b;
}

__host__ __device__ uint1_t_x8 operator/(const uint1_t_x8& a, const uint1_t_x8& b);

__host__ __device__ uint1_t_x8& operator&=(uint1_t_x8& a, const uint1_t_x8& b)
{
return (a = a & b);
}

__host__ __device__ uint1_t_x8& operator|=(uint1_t_x8& a, const uint1_t_x8& b)
{
return (a = a | b);
}

__host__ __device__ uint1_t_x8& operator^=(uint1_t_x8& a, const uint1_t_x8& b)
{
return (a = a ^ b);
}

__host__ __device__ uint1_t_x8& operator+=(uint1_t_x8& a, const uint1_t_x8& b)
{
return (a = a + b);
}

__host__ __device__ uint1_t_x8& operator-=(uint1_t_x8& a, const uint1_t_x8& b)
{
return (a = a - b);
}

__host__ __device__ uint1_t_x8& operator*=(uint1_t_x8& a, const uint1_t_x8& b)
{
return (a = a * b);
}

__host__ __device__ uint1_t_x8& operator/=(uint1_t_x8& a, const uint1_t_x8& b);

namespace binary_tensor
{
namespace dtype
{
__host__ __device__ uint1_t_x8 operator&(const uint1_t_x8& a, const uint1_t_x8& b)
{
return static_cast<const unsigned char&>(a) & static_cast<const unsigned char&>(b);
}

__host__ __device__ uint1_t_x8 operator|(const uint1_t_x8& a, const uint1_t_x8& b)
{
return static_cast<const unsigned char&>(a) | static_cast<const unsigned char&>(b);
}

__host__ __device__ uint1_t_x8 operator^(const uint1_t_x8& a, const uint1_t_x8& b)
{
return static_cast<const unsigned char&>(a) ^ static_cast<const unsigned char&>(b);
}

__host__ __device__ uint1_t_x8 operator~(const uint1_t_x8& values)
{
return ~static_cast<const unsigned char&>(values);
}

__host__ __device__ uint1_t_x8 operator+(const uint1_t_x8& a, const uint1_t_x8& b)
{
return a ^ b;
}

__host__ __device__ uint1_t_x8 operator-(const uint1_t_x8& a, const uint1_t_x8& b)
{
return a ^ b;
}

__host__ __device__ uint1_t_x8 operator*(const uint1_t_x8& a, const uint1_t_x8& b)
{
return a & b;
}

__host__ __device__ uint1_t_x8 operator/(const uint1_t_x8& a, const uint1_t_x8& b);

__host__ __device__ uint1_t_x8& operator&=(uint1_t_x8& a, const uint1_t_x8& b)
{
return (a = a & b);
}

__host__ __device__ uint1_t_x8& operator|=(uint1_t_x8& a, const uint1_t_x8& b)
{
return (a = a | b);
}

__host__ __device__ uint1_t_x8& operator^=(uint1_t_x8& a, const uint1_t_x8& b)
{
return (a = a ^ b);
}

__host__ __device__ uint1_t_x8& operator+=(uint1_t_x8& a, const uint1_t_x8& b)
{
return (a = a + b);
}

__host__ __device__ uint1_t_x8& operator-=(uint1_t_x8& a, const uint1_t_x8& b)
{
return (a = a - b);
}

__host__ __device__ uint1_t_x8& operator*=(uint1_t_x8& a, const uint1_t_x8& b)
{
return (a = a * b);
}

__host__ __device__ uint1_t_x8& operator/=(uint1_t_x8& a, const uint1_t_x8& b);

}
}
Loading

0 comments on commit 341cb40

Please sign in to comment.