-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Re-structure the neural network package
Re-structure the `nn` package to: * Feature an external half precision module since it is used in multiple separate modules. * Use half precision in the weight JSON format to decrease the disk size of the file (88Mb -> 44Mb) * Move the ffi interface to NVIDIA CUDA into an internal `ffi` module that is private to the `nn` package. * Improve error checking of CUDA.
- Loading branch information
Showing
13 changed files
with
495 additions
and
407 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright 2017 Karl Sundequist Blomdahl <karl.sundequist.blomdahl@gmail.com> | ||
// | ||
// 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. | ||
|
||
/// 16-bit floating point numbers as defined in IEEE 754-2008. | ||
#[allow(non_camel_case_types)] | ||
pub struct f16(u16); | ||
|
||
impl f16 { | ||
/// Wrap the given bits as an half precision floating point number. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `bits` - the bits to wrap | ||
/// | ||
pub fn from_bits(bits: u16) -> f16 { | ||
f16(bits) | ||
} | ||
|
||
/// Returns the wrapped bits. | ||
pub fn to_bits(&self) -> u16 { | ||
let f16(bits) = *self; | ||
|
||
bits | ||
} | ||
} | ||
|
||
extern "C" { | ||
#[link_name = "llvm.convert.to.fp16.f32"] | ||
fn convert_to_fp16_f32(f: f32) -> u16; | ||
|
||
#[link_name = "llvm.convert.from.fp16.f32"] | ||
fn convert_from_fp16_f32(f: u16) -> f32; | ||
} | ||
|
||
impl From<f16> for f32 { | ||
fn from(value: f16) -> f32 { | ||
let f16(bits) = value; | ||
|
||
unsafe { convert_from_fp16_f32(bits) } | ||
} | ||
} | ||
|
||
impl From<f32> for f16 { | ||
fn from(value: f32) -> f16 { | ||
f16(unsafe { convert_to_fp16_f32(value) }) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use ::f16::*; | ||
|
||
#[test] | ||
fn from_f16_to_f32() { | ||
assert_eq!(f32::from(f16::from_bits(0x4170)), 2.71875); // e | ||
assert_eq!(f32::from(f16::from_bits(0x4248)), 3.140625); // pi | ||
assert_eq!(f32::from(f16::from_bits(0x3518)), 0.31835938); // 1/pi | ||
assert_eq!(f32::from(f16::from_bits(0x398c)), 0.6933594); // ln 2 | ||
assert_eq!(f32::from(f16::from_bits(0x36f3)), 0.43432617); // log10 e | ||
assert_eq!(f32::from(f16::from_bits(0x3dc5)), 1.4423828); // log2 e | ||
assert_eq!(f32::from(f16::from_bits(0x3da8)), 1.4140625); // sqrt 2 | ||
} | ||
|
||
#[test] | ||
fn from_f32_to_f16() { | ||
assert_eq!(f16::from(::std::f32::consts::PI).to_bits(), 0x4248); // pi | ||
assert_eq!(f16::from(::std::f32::consts::E).to_bits(), 0x4170); // pi | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ extern crate regex; | |
extern crate ordered_float; | ||
|
||
pub mod dataset; | ||
mod f16; | ||
pub mod go; | ||
pub mod mcts; | ||
pub mod nn; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// Copyright 2017 Karl Sundequist Blomdahl <karl.sundequist.blomdahl@gmail.com> | ||
// | ||
// 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. | ||
|
||
pub mod cublas; | ||
pub mod cuda; | ||
pub mod cudnn; | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use libc::{c_void}; | ||
use std::ptr; | ||
|
||
use ::nn::ffi::*; | ||
|
||
#[test] | ||
fn sgemm() { | ||
let mut handle: cublas::Handle = ptr::null_mut(); | ||
let c_0 = 0.0f32; | ||
let c_1 = 1.0f32; | ||
|
||
unsafe { | ||
let a = [ // 3x2 | ||
1.0f32, 2.0f32, | ||
3.0f32, 4.0f32, | ||
5.0f32, 6.0f32 | ||
]; | ||
let b = [ // 2x3 | ||
1.0f32, 2.0f32, 3.0f32, | ||
4.0f32, 5.0f32, 6.0f32 | ||
]; | ||
let c = [ // 3x3 | ||
0.0f32, 0.0f32, 0.0f32, | ||
0.0f32, 0.0f32, 0.0f32, | ||
0.0f32, 0.0f32, 0.0f32 | ||
]; | ||
|
||
// C = A * B | ||
let mut a_ = ptr::null_mut(); | ||
let mut b_ = ptr::null_mut(); | ||
let mut c_ = ptr::null_mut(); | ||
|
||
assert_eq!(cuda::cudaMalloc(&mut a_, 24), cuda::Error::Success); | ||
assert_eq!(cuda::cudaMalloc(&mut b_, 24), cuda::Error::Success); | ||
assert_eq!(cuda::cudaMalloc(&mut c_, 36), cuda::Error::Success); | ||
assert_eq!(cuda::cudaMemcpy( | ||
a_, | ||
a.as_ptr() as *const c_void, | ||
24, | ||
cuda::MemcpyKind::HostToDevice | ||
), cuda::Error::Success); | ||
assert_eq!(cuda::cudaMemcpy( | ||
b_, | ||
b.as_ptr() as *const c_void, | ||
24, | ||
cuda::MemcpyKind::HostToDevice | ||
), cuda::Error::Success); | ||
|
||
assert_eq!(cublas::cublasCreate_v2(&mut handle), cublas::Status::Success); | ||
assert_eq!(cublas::cublasSgemm_v2( | ||
handle, | ||
cublas::Operation::N, | ||
cublas::Operation::N, | ||
3, 3, 2, | ||
&c_1, | ||
b_, 3, | ||
a_, 2, | ||
&c_0, | ||
c_, 3 | ||
), cublas::Status::Success); | ||
assert_eq!(cublas::cublasDestroy_v2(handle), cublas::Status::Success); | ||
|
||
// check the results | ||
assert_eq!(cuda::cudaMemcpy( | ||
c.as_ptr() as *mut c_void, | ||
c_, | ||
36, | ||
cuda::MemcpyKind::DeviceToHost | ||
), cuda::Error::Success); | ||
|
||
assert_eq!(c, [ | ||
9.0f32, 12.0f32, 15.0f32, | ||
19.0f32, 26.0f32, 33.0f32, | ||
29.0f32, 40.0f32, 51.0f32 | ||
]) | ||
} | ||
} | ||
} |
Oops, something went wrong.