#include <cstdio>

class Layer_info {
    public:
    int M, N, O;

    float *opt;
    float *preact;

    float *bias;
    float *weight;

    float *bp_opt;
    float *bp_preact;
    float *bp_weight;

    Layer_info(int M, int N, int O);

    ~Layer_info();

    void setOutput(float *data);
    void clear();
    void bp_clear();
    void save_model(FILE*);
    void read_model(FILE*);
};

// Constructor
Layer_info::Layer_info(int M, int N, int O)
{
    this->M = M;
    this->N = N;
    this->O = O;

    float h_bias[N];
    float h_weight[N][M];

    opt = NULL;
    preact = NULL;
    bias   = NULL;
    weight = NULL;

    for (int i = 0; i < N; ++i) {
        h_bias[i] = 0.5f - float(rand()) / float(RAND_MAX);
        /*h_bias[i] = 0.0f;*/

        for (int j = 0; j < M; ++j) {
            h_weight[i][j] = 0.5f - float(rand()) / float(RAND_MAX);
            /*h_weight[i][j] = 0.05f;*/
        }
    }

    cudaMalloc(&opt, sizeof(float) * O);
    cudaMalloc(&preact, sizeof(float) * O);

    cudaMalloc(&bias, sizeof(float) * N);

    cudaMalloc(&weight, sizeof(float) * M * N);

    cudaMalloc(&bp_opt, sizeof(float) * O);
    cudaMalloc(&bp_preact, sizeof(float) * O);
    cudaMalloc(&bp_weight, sizeof(float) * M * N);

    cudaMemcpy(bias, h_bias, sizeof(float) * N, cudaMemcpyHostToDevice);

    cudaMemcpy(weight, h_weight, sizeof(float) * M * N, cudaMemcpyHostToDevice);
}

// Destructor
Layer_info::~Layer_info()
{
    cudaFree(opt);
    cudaFree(preact);

    cudaFree(bias);

    cudaFree(weight);

    cudaFree(bp_opt);
    cudaFree(bp_preact);
    cudaFree(bp_weight);
}

// Send data one row from dataset to the GPU
void Layer_info::setOutput(float *data)
{
    cudaMemcpy(opt, data, sizeof(float) * O, cudaMemcpyHostToDevice);
}

// Reset GPU memory between iterations
void Layer_info::clear()
{
    cudaMemset(opt, 0x00, sizeof(float) * O);
    cudaMemset(preact, 0x00, sizeof(float) * O);
}

void Layer_info::bp_clear()
{
    cudaMemset(bp_opt, 0x00, sizeof(float) * O);
    cudaMemset(bp_preact, 0x00, sizeof(float) * O);
    cudaMemset(bp_weight, 0x00, sizeof(float) * M * N);
}

void Layer_info::save_model(FILE *model)
{
    char buffer[100000];
    fwrite((char*)&M, sizeof(int), 1, model);
    fwrite((char*)&N, sizeof(int), 1, model);

    cudaMemcpy(buffer, (char*)bias, sizeof(float) * N, cudaMemcpyDeviceToHost);
    fwrite(buffer, sizeof(float) * N, 1, model);
    cudaMemcpy(buffer, (char*)weight, sizeof(float) * M * N, cudaMemcpyDeviceToHost);
    fwrite(buffer, sizeof(float) * M * N, 1, model);
}

void Layer_info::read_model(FILE *model)
{
    char buffer[100000];
    
    fread((char*)&M, sizeof(int), 1, model);
    fread((char*)&N, sizeof(int), 1, model);
    
    fread(buffer, sizeof(float) * N, 1, model);
    cudaMemcpy(bias, (float*)buffer, sizeof(float) * N, cudaMemcpyHostToDevice);
    fread(buffer, sizeof(float) * M * N, 1, model);
    cudaMemcpy(weight, (float*)buffer, sizeof(float) * M * N, cudaMemcpyHostToDevice);
}

	.file	"tmpxft_002e147f_00000000-6_layer.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2040:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2040:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.align 2
	.globl	_ZN10Layer_infoC2Eiii
	.type	_ZN10Layer_infoC2Eiii, @function
_ZN10Layer_infoC2Eiii:
.LFB2028:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	pushq	%r15
	pushq	%r14
	.cfi_offset 15, -24
	.cfi_offset 14, -32
	movl	%esi, %r14d
	pushq	%r13
	.cfi_offset 13, -40
	movl	%edx, %r13d
	pushq	%r12
	pushq	%rbx
	.cfi_offset 12, -48
	.cfi_offset 3, -56
	movq	%rdi, %rbx
	subq	$88, %rsp
	movl	%ecx, -84(%rbp)
	movq	%fs:40, %rax
	movq	%rax, -56(%rbp)
	xorl	%eax, %eax
	movslq	%edx, %rax
	movl	%edx, 4(%rdi)
	salq	$2, %rax
	movl	%esi, (%rdi)
	leaq	15(%rax), %rdx
	movl	%ecx, 8(%rdi)
	movq	%rsp, %rcx
	movq	%rax, -72(%rbp)
	movq	%rdx, %rax
	andq	$-4096, %rdx
	andq	$-16, %rax
	subq	%rdx, %rcx
.L3:
	cmpq	%rcx, %rsp
	je	.L4
	subq	$4096, %rsp
	orq	$0, 4088(%rsp)
	jmp	.L3
.L4:
	andl	$4095, %eax
	subq	%rax, %rsp
	testq	%rax, %rax
	je	.L5
	orq	$0, -8(%rsp,%rax)
.L5:
	movslq	%r14d, %rax
	movq	%rsp, -96(%rbp)
	movq	%rsp, %rcx
	leaq	0(,%rax,4), %rsi
	movq	%rsi, -112(%rbp)
	movq	-72(%rbp), %rsi
	imulq	%rsi, %rax
	leaq	15(%rax), %rdx
	movq	%rax, -80(%rbp)
	movq	%rdx, %rax
	andq	$-4096, %rdx
	andq	$-16, %rax
	subq	%rdx, %rcx
.L6:
	cmpq	%rcx, %rsp
	je	.L7
	subq	$4096, %rsp
	orq	$0, 4088(%rsp)
	jmp	.L6
.L7:
	andl	$4095, %eax
	subq	%rax, %rsp
	testq	%rax, %rax
	je	.L8
	orq	$0, -8(%rsp,%rax)
.L8:
	xorl	%eax, %eax
	movq	%rsp, -104(%rbp)
	movq	%rsp, %r15
	xorl	%r12d, %r12d
	movq	%rax, 16(%rbx)
	movq	%rax, 24(%rbx)
	movq	%rax, 32(%rbx)
	movq	%rax, 40(%rbx)
.L9:
	cmpl	%r12d, %r13d
	jle	.L22
	call	rand@PLT
	movss	.LC1(%rip), %xmm1
	xorl	%ecx, %ecx
	cvtsi2ssl	%eax, %xmm0
	mulss	.LC0(%rip), %xmm0
	movq	-96(%rbp), %rax
	subss	%xmm0, %xmm1
	movss	%xmm1, (%rax,%r12,4)
.L10:
	cmpl	%ecx, %r14d
	jle	.L23
	movq	%rcx, -120(%rbp)
	call	rand@PLT
	movss	.LC1(%rip), %xmm1
	movq	-120(%rbp), %rcx
	cvtsi2ssl	%eax, %xmm0
	mulss	.LC0(%rip), %xmm0
	subss	%xmm0, %xmm1
	movss	%xmm1, (%r15,%rcx,4)
	incq	%rcx
	jmp	.L10
.L23:
	movq	-112(%rbp), %rax
	incq	%r12
	addq	%rax, %r15
	jmp	.L9
.L22:
	movslq	-84(%rbp), %r12
	leaq	16(%rbx), %rdi
	salq	$2, %r12
	movq	%r12, %rsi
	call	cudaMalloc@PLT
	leaq	24(%rbx), %rdi
	movq	%r12, %rsi
	call	cudaMalloc@PLT
	movq	-72(%rbp), %rsi
	leaq	32(%rbx), %rdi
	call	cudaMalloc@PLT
	movq	-80(%rbp), %rsi
	leaq	40(%rbx), %rdi
	call	cudaMalloc@PLT
	leaq	48(%rbx), %rdi
	movq	%r12, %rsi
	call	cudaMalloc@PLT
	leaq	56(%rbx), %rdi
	movq	%r12, %rsi
	call	cudaMalloc@PLT
	movq	-80(%rbp), %rsi
	leaq	64(%rbx), %rdi
	call	cudaMalloc@PLT
	movq	32(%rbx), %rdi
	movq	-72(%rbp), %rdx
	movl	$1, %ecx
	movq	-96(%rbp), %rsi
	call	cudaMemcpy@PLT
	movq	40(%rbx), %rdi
	movq	-80(%rbp), %rdx
	movl	$1, %ecx
	movq	-104(%rbp), %rsi
	call	cudaMemcpy@PLT
	movq	-56(%rbp), %rax
	subq	%fs:40, %rax
	je	.L13
	call	__stack_chk_fail@PLT
.L13:
	leaq	-40(%rbp), %rsp
	popq	%rbx
	popq	%r12
	popq	%r13
	popq	%r14
	popq	%r15
	popq	%rbp
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE2028:
	.size	_ZN10Layer_infoC2Eiii, .-_ZN10Layer_infoC2Eiii
	.globl	_ZN10Layer_infoC1Eiii
	.set	_ZN10Layer_infoC1Eiii,_ZN10Layer_infoC2Eiii
	.align 2
	.globl	_ZN10Layer_infoD2Ev
	.type	_ZN10Layer_infoD2Ev, @function
_ZN10Layer_infoD2Ev:
.LFB2031:
	.cfi_startproc
	.cfi_personality 0x9b,DW.ref.__gxx_personality_v0
	.cfi_lsda 0x1b,.LLSDA2031
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	movq	%rdi, %rbx
	movq	16(%rdi), %rdi
	call	cudaFree@PLT
	movq	24(%rbx), %rdi
	call	cudaFree@PLT
	movq	32(%rbx), %rdi
	call	cudaFree@PLT
	movq	40(%rbx), %rdi
	call	cudaFree@PLT
	movq	48(%rbx), %rdi
	call	cudaFree@PLT
	movq	56(%rbx), %rdi
	call	cudaFree@PLT
	movq	64(%rbx), %rdi
	call	cudaFree@PLT
	popq	%rbx
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2031:
	.globl	__gxx_personality_v0
	.section	.gcc_except_table,"a",@progbits
.LLSDA2031:
	.byte	0xff
	.byte	0xff
	.byte	0x1
	.uleb128 .LLSDACSE2031-.LLSDACSB2031
.LLSDACSB2031:
.LLSDACSE2031:
	.text
	.size	_ZN10Layer_infoD2Ev, .-_ZN10Layer_infoD2Ev
	.globl	_ZN10Layer_infoD1Ev
	.set	_ZN10Layer_infoD1Ev,_ZN10Layer_infoD2Ev
	.align 2
	.globl	_ZN10Layer_info9setOutputEPf
	.type	_ZN10Layer_info9setOutputEPf, @function
_ZN10Layer_info9setOutputEPf:
.LFB2033:
	.cfi_startproc
	endbr64
	movslq	8(%rdi), %rdx
	movq	16(%rdi), %rdi
	movl	$1, %ecx
	salq	$2, %rdx
	jmp	cudaMemcpy@PLT
	.cfi_endproc
.LFE2033:
	.size	_ZN10Layer_info9setOutputEPf, .-_ZN10Layer_info9setOutputEPf
	.align 2
	.globl	_ZN10Layer_info5clearEv
	.type	_ZN10Layer_info5clearEv, @function
_ZN10Layer_info5clearEv:
.LFB2034:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	movslq	8(%rdi), %rdx
	movq	%rdi, %rbx
	xorl	%esi, %esi
	movq	16(%rdi), %rdi
	salq	$2, %rdx
	call	cudaMemset@PLT
	movslq	8(%rbx), %rdx
	movq	24(%rbx), %rdi
	xorl	%esi, %esi
	popq	%rbx
	.cfi_def_cfa_offset 8
	salq	$2, %rdx
	jmp	cudaMemset@PLT
	.cfi_endproc
.LFE2034:
	.size	_ZN10Layer_info5clearEv, .-_ZN10Layer_info5clearEv
	.align 2
	.globl	_ZN10Layer_info8bp_clearEv
	.type	_ZN10Layer_info8bp_clearEv, @function
_ZN10Layer_info8bp_clearEv:
.LFB2035:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	movslq	8(%rdi), %rdx
	movq	%rdi, %rbx
	xorl	%esi, %esi
	movq	48(%rdi), %rdi
	salq	$2, %rdx
	call	cudaMemset@PLT
	movslq	8(%rbx), %rdx
	movq	56(%rbx), %rdi
	xorl	%esi, %esi
	salq	$2, %rdx
	call	cudaMemset@PLT
	movslq	(%rbx), %rdx
	movslq	4(%rbx), %rax
	xorl	%esi, %esi
	movq	64(%rbx), %rdi
	popq	%rbx
	.cfi_def_cfa_offset 8
	imulq	%rax, %rdx
	salq	$2, %rdx
	jmp	cudaMemset@PLT
	.cfi_endproc
.LFE2035:
	.size	_ZN10Layer_info8bp_clearEv, .-_ZN10Layer_info8bp_clearEv
	.align 2
	.globl	_ZN10Layer_info10save_modelEP8_IO_FILE
	.type	_ZN10Layer_info10save_modelEP8_IO_FILE, @function
_ZN10Layer_info10save_modelEP8_IO_FILE:
.LFB2036:
	.cfi_startproc
	endbr64
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	pushq	%rbp
	.cfi_def_cfa_offset 24
	.cfi_offset 6, -24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset 3, -32
	leaq	-98304(%rsp), %r11
	.cfi_def_cfa 11, 98336
.LPSRL0:
	subq	$4096, %rsp
	orq	$0, (%rsp)
	cmpq	%r11, %rsp
	jne	.LPSRL0
	.cfi_def_cfa_register 7
	subq	$1712, %rsp
	.cfi_def_cfa_offset 100048
	movl	$1, %edx
	movq	%rdi, %rbx
	movq	%rsi, %rbp
	leaq	8(%rsp), %r12
	movq	%rsi, %rcx
	movq	%fs:40, %rax
	movq	%rax, 100008(%rsp)
	xorl	%eax, %eax
	movl	$4, %esi
	call	fwrite@PLT
	leaq	4(%rbx), %rdi
	movq	%rbp, %rcx
	movl	$1, %edx
	movl	$4, %esi
	call	fwrite@PLT
	movslq	4(%rbx), %rdx
	movq	32(%rbx), %rsi
	movq	%r12, %rdi
	movl	$2, %ecx
	salq	$2, %rdx
	call	cudaMemcpy@PLT
	movslq	4(%rbx), %rsi
	movq	%rbp, %rcx
	movq	%r12, %rdi
	movl	$1, %edx
	salq	$2, %rsi
	call	fwrite@PLT
	movslq	(%rbx), %rdx
	movslq	4(%rbx), %rax
	movq	%r12, %rdi
	movq	40(%rbx), %rsi
	movl	$2, %ecx
	imulq	%rax, %rdx
	salq	$2, %rdx
	call	cudaMemcpy@PLT
	movslq	(%rbx), %rsi
	movslq	4(%rbx), %rax
	movq	%rbp, %rcx
	movl	$1, %edx
	movq	%r12, %rdi
	imulq	%rax, %rsi
	salq	$2, %rsi
	call	fwrite@PLT
	movq	100008(%rsp), %rax
	subq	%fs:40, %rax
	je	.L32
	call	__stack_chk_fail@PLT
.L32:
	addq	$100016, %rsp
	.cfi_def_cfa_offset 32
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%rbp
	.cfi_def_cfa_offset 16
	popq	%r12
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2036:
	.size	_ZN10Layer_info10save_modelEP8_IO_FILE, .-_ZN10Layer_info10save_modelEP8_IO_FILE
	.align 2
	.globl	_ZN10Layer_info10read_modelEP8_IO_FILE
	.type	_ZN10Layer_info10read_modelEP8_IO_FILE, @function
_ZN10Layer_info10read_modelEP8_IO_FILE:
.LFB2037:
	.cfi_startproc
	endbr64
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	pushq	%rbp
	.cfi_def_cfa_offset 24
	.cfi_offset 6, -24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset 3, -32
	leaq	-98304(%rsp), %r11
	.cfi_def_cfa 11, 98336
.LPSRL1:
	subq	$4096, %rsp
	orq	$0, (%rsp)
	cmpq	%r11, %rsp
	jne	.LPSRL1
	.cfi_def_cfa_register 7
	subq	$1712, %rsp
	.cfi_def_cfa_offset 100048
	movl	$1, %ecx
	movl	$4, %edx
	movq	%rdi, %rbx
	movq	%rsi, %rbp
	leaq	8(%rsp), %r12
	movq	%rsi, %r8
	movq	%fs:40, %rax
	movq	%rax, 100008(%rsp)
	xorl	%eax, %eax
	orq	$-1, %rsi
	call	__fread_chk@PLT
	orq	$-1, %rsi
	leaq	4(%rbx), %rdi
	movq	%rbp, %r8
	movl	$1, %ecx
	movl	$4, %edx
	call	__fread_chk@PLT
	movslq	4(%rbx), %rdx
	movq	%rbp, %r8
	movq	%r12, %rdi
	movl	$1, %ecx
	movl	$100000, %esi
	salq	$2, %rdx
	call	__fread_chk@PLT
	movslq	4(%rbx), %rdx
	movq	32(%rbx), %rdi
	movq	%r12, %rsi
	movl	$1, %ecx
	salq	$2, %rdx
	call	cudaMemcpy@PLT
	movslq	(%rbx), %rdx
	movslq	4(%rbx), %rax
	movq	%r12, %rdi
	movl	$1, %ecx
	movl	$100000, %esi
	movq	%rbp, %r8
	imulq	%rax, %rdx
	salq	$2, %rdx
	call	__fread_chk@PLT
	movslq	(%rbx), %rdx
	movslq	4(%rbx), %rax
	movq	%r12, %rsi
	movq	40(%rbx), %rdi
	movl	$1, %ecx
	imulq	%rax, %rdx
	salq	$2, %rdx
	call	cudaMemcpy@PLT
	movq	100008(%rsp), %rax
	subq	%fs:40, %rax
	je	.L35
	call	__stack_chk_fail@PLT
.L35:
	addq	$100016, %rsp
	.cfi_def_cfa_offset 32
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%rbp
	.cfi_def_cfa_offset 16
	popq	%r12
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2037:
	.size	_ZN10Layer_info10read_modelEP8_IO_FILE, .-_ZN10Layer_info10read_modelEP8_IO_FILE
	.section	.text.startup,"ax",@progbits
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2063:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	movq	%rax, %rdi
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rdx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE2063:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.section	.rodata.cst4,"aM",@progbits,4
	.align 4
.LC0:
	.long	805306368
	.align 4
.LC1:
	.long	1056964608
	.hidden	DW.ref.__gxx_personality_v0
	.weak	DW.ref.__gxx_personality_v0
	.section	.data.rel.local.DW.ref.__gxx_personality_v0,"awG",@progbits,DW.ref.__gxx_personality_v0,comdat
	.align 8
	.type	DW.ref.__gxx_personality_v0, @object
	.size	DW.ref.__gxx_personality_v0, 8
DW.ref.__gxx_personality_v0:
	.quad	__gxx_personality_v0
	.ident	"GCC: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:


#include <hip/hip_runtime.h>
#include <cstdio>

class Layer_info {
    public:
    int M, N, O;

    float *opt;
    float *preact;

    float *bias;
    float *weight;

    float *bp_opt;
    float *bp_preact;
    float *bp_weight;

    Layer_info(int M, int N, int O);

    ~Layer_info();

    void setOutput(float *data);
    void clear();
    void bp_clear();
    void save_model(FILE*);
    void read_model(FILE*);
};

// Constructor
Layer_info::Layer_info(int M, int N, int O)
{
    this->M = M;
    this->N = N;
    this->O = O;

    float h_bias[N];
    float h_weight[N][M];

    opt = NULL;
    preact = NULL;
    bias   = NULL;
    weight = NULL;

    for (int i = 0; i < N; ++i) {
        h_bias[i] = 0.5f - float(rand()) / float(RAND_MAX);
        /*h_bias[i] = 0.0f;*/

        for (int j = 0; j < M; ++j) {
            h_weight[i][j] = 0.5f - float(rand()) / float(RAND_MAX);
            /*h_weight[i][j] = 0.05f;*/
        }
    }

    hipMalloc(&opt, sizeof(float) * O);
    hipMalloc(&preact, sizeof(float) * O);

    hipMalloc(&bias, sizeof(float) * N);

    hipMalloc(&weight, sizeof(float) * M * N);

    hipMalloc(&bp_opt, sizeof(float) * O);
    hipMalloc(&bp_preact, sizeof(float) * O);
    hipMalloc(&bp_weight, sizeof(float) * M * N);

    hipMemcpy(bias, h_bias, sizeof(float) * N, hipMemcpyHostToDevice);

    hipMemcpy(weight, h_weight, sizeof(float) * M * N, hipMemcpyHostToDevice);
}

// Destructor
Layer_info::~Layer_info()
{
    hipFree(opt);
    hipFree(preact);

    hipFree(bias);

    hipFree(weight);

    hipFree(bp_opt);
    hipFree(bp_preact);
    hipFree(bp_weight);
}

// Send data one row from dataset to the GPU
void Layer_info::setOutput(float *data)
{
    hipMemcpy(opt, data, sizeof(float) * O, hipMemcpyHostToDevice);
}

// Reset GPU memory between iterations
void Layer_info::clear()
{
    hipMemset(opt, 0x00, sizeof(float) * O);
    hipMemset(preact, 0x00, sizeof(float) * O);
}

void Layer_info::bp_clear()
{
    hipMemset(bp_opt, 0x00, sizeof(float) * O);
    hipMemset(bp_preact, 0x00, sizeof(float) * O);
    hipMemset(bp_weight, 0x00, sizeof(float) * M * N);
}

void Layer_info::save_model(FILE *model)
{
    char buffer[100000];
    fwrite((char*)&M, sizeof(int), 1, model);
    fwrite((char*)&N, sizeof(int), 1, model);

    hipMemcpy(buffer, (char*)bias, sizeof(float) * N, hipMemcpyDeviceToHost);
    fwrite(buffer, sizeof(float) * N, 1, model);
    hipMemcpy(buffer, (char*)weight, sizeof(float) * M * N, hipMemcpyDeviceToHost);
    fwrite(buffer, sizeof(float) * M * N, 1, model);
}

void Layer_info::read_model(FILE *model)
{
    char buffer[100000];
    
    fread((char*)&M, sizeof(int), 1, model);
    fread((char*)&N, sizeof(int), 1, model);
    
    fread(buffer, sizeof(float) * N, 1, model);
    hipMemcpy(bias, (float*)buffer, sizeof(float) * N, hipMemcpyHostToDevice);
    fread(buffer, sizeof(float) * M * N, 1, model);
    hipMemcpy(weight, (float*)buffer, sizeof(float) * M * N, hipMemcpyHostToDevice);
}

	.text
	.file	"layer.hip"
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function _ZN10Layer_infoC2Eiii
.LCPI0_0:
	.long	0xb0000000                      # float -4.65661287E-10
.LCPI0_1:
	.long	0x3f000000                      # float 0.5
	.text
	.globl	_ZN10Layer_infoC2Eiii
	.p2align	1, 0x90
	.type	_ZN10Layer_infoC2Eiii,@function
_ZN10Layer_infoC2Eiii:                  # @_ZN10Layer_infoC2Eiii
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset %rbp, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register %rbp
	pushq	%r15
	pushq	%r14
	pushq	%r13
	pushq	%r12
	pushq	%rbx
	subq	$56, %rsp
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	movl	%esi, (%rdi)
	movl	%edx, 4(%rdi)
	movl	%ecx, -60(%rbp)                 # 4-byte Spill
	movl	%ecx, 8(%rdi)
	movl	%edx, %r8d
	movq	%rsp, %rcx
	leaq	15(,%r8,4), %rax
	andq	$-16, %rax
	subq	%rax, %rcx
	movq	%rcx, -80(%rbp)                 # 8-byte Spill
	movq	%rcx, %rsp
	movl	%esi, -44(%rbp)                 # 4-byte Spill
	movl	%esi, %r13d
	movq	%r8, -56(%rbp)                  # 8-byte Spill
	movq	%r8, %rax
	imulq	%r13, %rax
	leaq	15(,%rax,4), %rax
	andq	$-16, %rax
	movq	%rsp, %rcx
	subq	%rax, %rcx
	movq	%rcx, -72(%rbp)                 # 8-byte Spill
	movq	%rcx, %rsp
	leaq	16(%rdi), %rax
	movq	%rax, -88(%rbp)                 # 8-byte Spill
	xorps	%xmm0, %xmm0
	movups	%xmm0, 32(%rdi)
	movq	%rdi, -96(%rbp)                 # 8-byte Spill
	movups	%xmm0, 16(%rdi)
	movl	%edx, -64(%rbp)                 # 4-byte Spill
	testl	%edx, %edx
	jle	.LBB0_6
# %bb.1:                                # %.lr.ph33
	leaq	(,%r13,4), %rbx
	xorl	%r14d, %r14d
	movq	-72(%rbp), %r12                 # 8-byte Reload
.LBB0_2:                                # =>This Loop Header: Depth=1
                                        #     Child Loop BB0_4 Depth 2
	callq	rand
	xorps	%xmm0, %xmm0
	cvtsi2ss	%eax, %xmm0
	movss	.LCPI0_0(%rip), %xmm1           # xmm1 = mem[0],zero,zero,zero
	mulss	%xmm1, %xmm0
	movss	.LCPI0_1(%rip), %xmm1           # xmm1 = mem[0],zero,zero,zero
	addss	%xmm1, %xmm0
	movq	-80(%rbp), %rax                 # 8-byte Reload
	movss	%xmm0, (%rax,%r14,4)
	cmpl	$0, -44(%rbp)                   # 4-byte Folded Reload
	jle	.LBB0_5
# %bb.3:                                # %.lr.ph
                                        #   in Loop: Header=BB0_2 Depth=1
	xorl	%r15d, %r15d
.LBB0_4:                                #   Parent Loop BB0_2 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	callq	rand
	movss	.LCPI0_0(%rip), %xmm1           # xmm1 = mem[0],zero,zero,zero
	xorps	%xmm0, %xmm0
	cvtsi2ss	%eax, %xmm0
	mulss	%xmm1, %xmm0
	movss	.LCPI0_1(%rip), %xmm1           # xmm1 = mem[0],zero,zero,zero
	addss	%xmm1, %xmm0
	movss	%xmm0, (%r12,%r15,4)
	incq	%r15
	cmpq	%r15, %r13
	jne	.LBB0_4
.LBB0_5:                                # %._crit_edge
                                        #   in Loop: Header=BB0_2 Depth=1
	incq	%r14
	addq	%rbx, %r12
	cmpq	-56(%rbp), %r14                 # 8-byte Folded Reload
	jne	.LBB0_2
.LBB0_6:                                # %._crit_edge34
	movq	-96(%rbp), %r14                 # 8-byte Reload
	leaq	40(%r14), %rax
	movq	%rax, -56(%rbp)                 # 8-byte Spill
	leaq	32(%r14), %r12
	leaq	24(%r14), %r13
	movslq	-60(%rbp), %r15                 # 4-byte Folded Reload
	shlq	$2, %r15
	movq	-88(%rbp), %rdi                 # 8-byte Reload
	movq	%r15, %rsi
	callq	hipMalloc
	movq	%r13, %rdi
	movq	%r15, %rsi
	callq	hipMalloc
	movslq	-64(%rbp), %rbx                 # 4-byte Folded Reload
	leaq	(,%rbx,4), %r13
	movq	%r12, %rdi
	movq	%r13, %rsi
	callq	hipMalloc
	movslq	-44(%rbp), %r12                 # 4-byte Folded Reload
	imulq	%rbx, %r12
	shlq	$2, %r12
	movq	-56(%rbp), %rdi                 # 8-byte Reload
	movq	%r12, %rsi
	callq	hipMalloc
	leaq	48(%r14), %rdi
	movq	%r15, %rsi
	callq	hipMalloc
	leaq	56(%r14), %rdi
	movq	%r15, %rsi
	callq	hipMalloc
	leaq	64(%r14), %rdi
	movq	%r12, %rsi
	callq	hipMalloc
	movq	32(%r14), %rdi
	movq	-80(%rbp), %rsi                 # 8-byte Reload
	movq	%r13, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movq	40(%r14), %rdi
	movq	-72(%rbp), %rsi                 # 8-byte Reload
	movq	%r12, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	leaq	-40(%rbp), %rsp
	popq	%rbx
	popq	%r12
	popq	%r13
	popq	%r14
	popq	%r15
	popq	%rbp
	.cfi_def_cfa %rsp, 8
	retq
.Lfunc_end0:
	.size	_ZN10Layer_infoC2Eiii, .Lfunc_end0-_ZN10Layer_infoC2Eiii
	.cfi_endproc
                                        # -- End function
	.globl	_ZN10Layer_infoD2Ev             # -- Begin function _ZN10Layer_infoD2Ev
	.p2align	1, 0x90
	.type	_ZN10Layer_infoD2Ev,@function
_ZN10Layer_infoD2Ev:                    # @_ZN10Layer_infoD2Ev
.Lfunc_begin0:
	.cfi_startproc
	.cfi_personality 3, __gxx_personality_v0
	.cfi_lsda 3, .Lexception0
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset %rbx, -16
	movq	%rdi, %rbx
	movq	16(%rdi), %rdi
.Ltmp0:
	callq	hipFree
.Ltmp1:
# %bb.1:
	movq	24(%rbx), %rdi
.Ltmp2:
	callq	hipFree
.Ltmp3:
# %bb.2:
	movq	32(%rbx), %rdi
.Ltmp4:
	callq	hipFree
.Ltmp5:
# %bb.3:
	movq	40(%rbx), %rdi
.Ltmp6:
	callq	hipFree
.Ltmp7:
# %bb.4:
	movq	48(%rbx), %rdi
.Ltmp8:
	callq	hipFree
.Ltmp9:
# %bb.5:
	movq	56(%rbx), %rdi
.Ltmp10:
	callq	hipFree
.Ltmp11:
# %bb.6:
	movq	64(%rbx), %rdi
.Ltmp12:
	callq	hipFree
.Ltmp13:
# %bb.7:
	popq	%rbx
	.cfi_def_cfa_offset 8
	retq
.LBB1_8:
	.cfi_def_cfa_offset 16
.Ltmp14:
	movq	%rax, %rdi
	callq	__clang_call_terminate
.Lfunc_end1:
	.size	_ZN10Layer_infoD2Ev, .Lfunc_end1-_ZN10Layer_infoD2Ev
	.cfi_endproc
	.section	.gcc_except_table,"a",@progbits
	.p2align	2, 0x0
GCC_except_table1:
.Lexception0:
	.byte	255                             # @LPStart Encoding = omit
	.byte	3                               # @TType Encoding = udata4
	.uleb128 .Lttbase0-.Lttbaseref0
.Lttbaseref0:
	.byte	1                               # Call site Encoding = uleb128
	.uleb128 .Lcst_end0-.Lcst_begin0
.Lcst_begin0:
	.uleb128 .Ltmp0-.Lfunc_begin0           # >> Call Site 1 <<
	.uleb128 .Ltmp13-.Ltmp0                 #   Call between .Ltmp0 and .Ltmp13
	.uleb128 .Ltmp14-.Lfunc_begin0          #     jumps to .Ltmp14
	.byte	1                               #   On action: 1
.Lcst_end0:
	.byte	1                               # >> Action Record 1 <<
                                        #   Catch TypeInfo 1
	.byte	0                               #   No further actions
	.p2align	2, 0x0
                                        # >> Catch TypeInfos <<
	.long	0                               # TypeInfo 1
.Lttbase0:
	.p2align	2, 0x0
                                        # -- End function
	.section	.text.__clang_call_terminate,"axG",@progbits,__clang_call_terminate,comdat
	.hidden	__clang_call_terminate          # -- Begin function __clang_call_terminate
	.weak	__clang_call_terminate
	.type	__clang_call_terminate,@function
__clang_call_terminate:                 # @__clang_call_terminate
	.cfi_startproc
# %bb.0:
	pushq	%rax
	.cfi_def_cfa_offset 16
	callq	__cxa_begin_catch
	callq	_ZSt9terminatev
.Lfunc_end2:
	.size	__clang_call_terminate, .Lfunc_end2-__clang_call_terminate
	.cfi_endproc
                                        # -- End function
	.text
	.globl	_ZN10Layer_info9setOutputEPf    # -- Begin function _ZN10Layer_info9setOutputEPf
	.p2align	1, 0x90
	.type	_ZN10Layer_info9setOutputEPf,@function
_ZN10Layer_info9setOutputEPf:           # @_ZN10Layer_info9setOutputEPf
	.cfi_startproc
# %bb.0:
	movq	16(%rdi), %rax
	movslq	8(%rdi), %rdx
	shlq	$2, %rdx
	movq	%rax, %rdi
	movl	$1, %ecx
	jmp	hipMemcpy                       # TAILCALL
.Lfunc_end3:
	.size	_ZN10Layer_info9setOutputEPf, .Lfunc_end3-_ZN10Layer_info9setOutputEPf
	.cfi_endproc
                                        # -- End function
	.globl	_ZN10Layer_info5clearEv         # -- Begin function _ZN10Layer_info5clearEv
	.p2align	1, 0x90
	.type	_ZN10Layer_info5clearEv,@function
_ZN10Layer_info5clearEv:                # @_ZN10Layer_info5clearEv
	.cfi_startproc
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset %rbx, -16
	movq	%rdi, %rbx
	movq	16(%rdi), %rdi
	movslq	8(%rbx), %rdx
	shlq	$2, %rdx
	xorl	%esi, %esi
	callq	hipMemset
	movq	24(%rbx), %rdi
	movslq	8(%rbx), %rdx
	shlq	$2, %rdx
	xorl	%esi, %esi
	popq	%rbx
	.cfi_def_cfa_offset 8
	jmp	hipMemset                       # TAILCALL
.Lfunc_end4:
	.size	_ZN10Layer_info5clearEv, .Lfunc_end4-_ZN10Layer_info5clearEv
	.cfi_endproc
                                        # -- End function
	.globl	_ZN10Layer_info8bp_clearEv      # -- Begin function _ZN10Layer_info8bp_clearEv
	.p2align	1, 0x90
	.type	_ZN10Layer_info8bp_clearEv,@function
_ZN10Layer_info8bp_clearEv:             # @_ZN10Layer_info8bp_clearEv
	.cfi_startproc
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset %rbx, -16
	movq	%rdi, %rbx
	movq	48(%rdi), %rdi
	movslq	8(%rbx), %rdx
	shlq	$2, %rdx
	xorl	%esi, %esi
	callq	hipMemset
	movq	56(%rbx), %rdi
	movslq	8(%rbx), %rdx
	shlq	$2, %rdx
	xorl	%esi, %esi
	callq	hipMemset
	movq	64(%rbx), %rdi
	movslq	(%rbx), %rax
	movslq	4(%rbx), %rdx
	imulq	%rax, %rdx
	shlq	$2, %rdx
	xorl	%esi, %esi
	popq	%rbx
	.cfi_def_cfa_offset 8
	jmp	hipMemset                       # TAILCALL
.Lfunc_end5:
	.size	_ZN10Layer_info8bp_clearEv, .Lfunc_end5-_ZN10Layer_info8bp_clearEv
	.cfi_endproc
                                        # -- End function
	.globl	_ZN10Layer_info10save_modelEP8_IO_FILE # -- Begin function _ZN10Layer_info10save_modelEP8_IO_FILE
	.p2align	1, 0x90
	.type	_ZN10Layer_info10save_modelEP8_IO_FILE,@function
_ZN10Layer_info10save_modelEP8_IO_FILE: # @_ZN10Layer_info10save_modelEP8_IO_FILE
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	subq	$100000, %rsp                   # imm = 0x186A0
	.cfi_def_cfa_offset 100032
	.cfi_offset %rbx, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movq	%rsi, %rbx
	movq	%rdi, %r14
	movl	$4, %esi
	movl	$1, %edx
	movq	%rbx, %rcx
	callq	fwrite
	leaq	4(%r14), %rdi
	movl	$4, %esi
	movl	$1, %edx
	movq	%rbx, %rcx
	callq	fwrite
	movq	32(%r14), %rsi
	movslq	4(%r14), %rdx
	shlq	$2, %rdx
	movq	%rsp, %r15
	movq	%r15, %rdi
	movl	$2, %ecx
	callq	hipMemcpy
	movslq	4(%r14), %rsi
	shlq	$2, %rsi
	movl	$1, %edx
	movq	%r15, %rdi
	movq	%rbx, %rcx
	callq	fwrite
	movq	40(%r14), %rsi
	movslq	(%r14), %rax
	movslq	4(%r14), %rdx
	imulq	%rax, %rdx
	shlq	$2, %rdx
	movq	%r15, %rdi
	movl	$2, %ecx
	callq	hipMemcpy
	movslq	(%r14), %rax
	movslq	4(%r14), %rsi
	imulq	%rax, %rsi
	shlq	$2, %rsi
	movl	$1, %edx
	movq	%r15, %rdi
	movq	%rbx, %rcx
	callq	fwrite
	addq	$100000, %rsp                   # imm = 0x186A0
	.cfi_def_cfa_offset 32
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end6:
	.size	_ZN10Layer_info10save_modelEP8_IO_FILE, .Lfunc_end6-_ZN10Layer_info10save_modelEP8_IO_FILE
	.cfi_endproc
                                        # -- End function
	.globl	_ZN10Layer_info10read_modelEP8_IO_FILE # -- Begin function _ZN10Layer_info10read_modelEP8_IO_FILE
	.p2align	1, 0x90
	.type	_ZN10Layer_info10read_modelEP8_IO_FILE,@function
_ZN10Layer_info10read_modelEP8_IO_FILE: # @_ZN10Layer_info10read_modelEP8_IO_FILE
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	subq	$100000, %rsp                   # imm = 0x186A0
	.cfi_def_cfa_offset 100032
	.cfi_offset %rbx, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movq	%rsi, %r14
	movq	%rdi, %rbx
	movl	$4, %esi
	movl	$1, %edx
	movq	%r14, %rcx
	callq	fread
	leaq	4(%rbx), %rdi
	movl	$4, %esi
	movl	$1, %edx
	movq	%r14, %rcx
	callq	fread
	movslq	4(%rbx), %rsi
	shlq	$2, %rsi
	movq	%rsp, %r15
	movl	$1, %edx
	movq	%r15, %rdi
	movq	%r14, %rcx
	callq	fread
	movq	32(%rbx), %rdi
	movslq	4(%rbx), %rdx
	shlq	$2, %rdx
	movq	%r15, %rsi
	movl	$1, %ecx
	callq	hipMemcpy
	movslq	(%rbx), %rax
	movslq	4(%rbx), %rsi
	imulq	%rax, %rsi
	shlq	$2, %rsi
	movl	$1, %edx
	movq	%r15, %rdi
	movq	%r14, %rcx
	callq	fread
	movq	40(%rbx), %rdi
	movslq	(%rbx), %rax
	movslq	4(%rbx), %rdx
	imulq	%rax, %rdx
	shlq	$2, %rdx
	movq	%r15, %rsi
	movl	$1, %ecx
	callq	hipMemcpy
	addq	$100000, %rsp                   # imm = 0x186A0
	.cfi_def_cfa_offset 32
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end7:
	.size	_ZN10Layer_info10read_modelEP8_IO_FILE, .Lfunc_end7-_ZN10Layer_info10read_modelEP8_IO_FILE
	.cfi_endproc
                                        # -- End function
	.type	__hip_cuid_,@object             # @__hip_cuid_
	.bss
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               # 0x0
	.size	__hip_cuid_, 1

	.globl	_ZN10Layer_infoC1Eiii
	.type	_ZN10Layer_infoC1Eiii,@function
.set _ZN10Layer_infoC1Eiii, _ZN10Layer_infoC2Eiii
	.globl	_ZN10Layer_infoD1Ev
	.type	_ZN10Layer_infoD1Ev,@function
.set _ZN10Layer_infoD1Ev, _ZN10Layer_infoD2Ev
	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __gxx_personality_v0
	.addrsig_sym __hip_cuid_

	.text
	.p2alignl 7, 3214868480
	.fill 96, 4, 3214868480
	.type	__hip_cuid_,@object             ; @__hip_cuid_
	.section	.bss,"aw",@nobits
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               ; 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.amdgpu_metadata
---
amdhsa.kernels:  []
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
