Skip to content

Commit 40609cb

Browse files
authored
Merge pull request #42 from iboB/msvc-build
ref #5 : MSVC build
2 parents 8d94358 + 0b45d25 commit 40609cb

File tree

6 files changed

+95
-24
lines changed

6 files changed

+95
-24
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@ stream
44
*.o
55
.cache
66
build/
7+
out/
8+
.vs/
9+
.vscode/
710
compile_commands.json

CMakeLists.txt

+25-17
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,21 @@ option(WHISPER_SUPPORT_SDL2 "whisper: support for libSDL2" OFF)
2626

2727
# sanitizers
2828

29-
if (WHISPER_SANITIZE_THREAD)
30-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=thread")
31-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread")
32-
endif()
29+
if (NOT MSVC)
30+
if (WHISPER_SANITIZE_THREAD)
31+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=thread")
32+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread")
33+
endif()
3334

34-
if (WHISPER_SANITIZE_ADDRESS)
35-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -fno-omit-frame-pointer")
36-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fno-omit-frame-pointer")
37-
endif()
35+
if (WHISPER_SANITIZE_ADDRESS)
36+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -fno-omit-frame-pointer")
37+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fno-omit-frame-pointer")
38+
endif()
3839

39-
if (WHISPER_SANITIZE_UNDEFINED)
40-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=undefined")
41-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined")
40+
if (WHISPER_SANITIZE_UNDEFINED)
41+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=undefined")
42+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined")
43+
endif()
4244
endif()
4345

4446
#set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ffast-math")
@@ -47,7 +49,7 @@ endif()
4749
# dependencies
4850

4951
set(CMAKE_C_STANDARD 11)
50-
set(CMAKE_CXX_STANDARD 11)
52+
set(CMAKE_CXX_STANDARD 20)
5153

5254
find_package(Threads REQUIRED)
5355

@@ -69,7 +71,7 @@ if (NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
6971
endif ()
7072

7173
if (WHISPER_ALL_WARNINGS)
72-
if (CMAKE_COMPILER_IS_GNUCC OR CMAKE_C_COMPILER_ID MATCHES "Clang")
74+
if (NOT MSVC)
7375
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} \
7476
-Wall \
7577
-Wextra \
@@ -80,20 +82,26 @@ if (WHISPER_ALL_WARNINGS)
8082
-Wpointer-arith \
8183
")
8284
else()
83-
# todo : windows
85+
# todo : msvc
8486
endif()
8587
endif()
8688

87-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror=vla")
88-
#set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-math-errno -ffinite-math-only -funsafe-math-optimizations")
89+
if (NOT MSVC)
90+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror=vla")
91+
#set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-math-errno -ffinite-math-only -funsafe-math-optimizations")
92+
endif()
8993

9094
message(STATUS "CMAKE_SYSTEM_PROCESSOR: ${CMAKE_SYSTEM_PROCESSOR}")
9195

9296
if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "arm" OR ${CMAKE_SYSTEM_PROCESSOR} MATCHES "aarch64")
9397
message(STATUS "ARM detected")
9498
else()
9599
message(STATUS "x86 detected")
96-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mavx -mavx2 -mfma -mf16c")
100+
if (MSVC)
101+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /arch:AVX2 /D_CRT_SECURE_NO_WARNINGS=1")
102+
else()
103+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mavx -mavx2 -mfma -mf16c")
104+
endif()
97105
endif()
98106

99107
# whisper - this is the main library of the project

ggml.c

+31-5
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,15 @@
1313
#include <string.h>
1414
#include <stdint.h>
1515
#include <stdio.h>
16-
#include <stdatomic.h>
1716

17+
18+
#if defined _MSC_VER
19+
#include "msvc_thread_atomic.h"
20+
#else
1821
#include <pthread.h>
22+
#include <stdatomic.h>
23+
typedef void* thread_ret_t;
24+
#endif
1925

2026
#define GGML_DEBUG 0
2127

@@ -149,6 +155,25 @@ static ggml_fp16_t table_exp_f16[1 << 16];
149155
// timing
150156
//
151157

158+
#if defined(_MSC_VER)
159+
static int64_t timer_freq;
160+
void ggml_time_init(void) {
161+
LARGE_INTEGER frequency;
162+
QueryPerformanceFrequency(&frequency);
163+
timer_freq = frequency.QuadPart;
164+
}
165+
int64_t ggml_time_ms(void) {
166+
LARGE_INTEGER t;
167+
QueryPerformanceCounter(&t);
168+
return (t.QuadPart * 1000) / timer_freq;
169+
}
170+
int64_t ggml_time_us(void) {
171+
LARGE_INTEGER t;
172+
QueryPerformanceCounter(&t);
173+
return (t.QuadPart * 1000000) / timer_freq;
174+
}
175+
#else
176+
void ggml_time_init(void) {}
152177
int64_t ggml_time_ms(void) {
153178
struct timespec ts;
154179
clock_gettime(CLOCK_MONOTONIC, &ts);
@@ -160,6 +185,7 @@ int64_t ggml_time_us(void) {
160185
clock_gettime(CLOCK_MONOTONIC, &ts);
161186
return (int64_t)ts.tv_sec*1000000 + (int64_t)ts.tv_nsec/1000;
162187
}
188+
#endif
163189

164190
int64_t ggml_cycles(void) {
165191
return clock();
@@ -6412,7 +6438,7 @@ void * ggml_graph_compute_one(void * data) {
64126438
return NULL;
64136439
}
64146440

6415-
void * ggml_graph_compute_thread(void * data) {
6441+
thread_ret_t ggml_graph_compute_thread(void * data) {
64166442
struct ggml_compute_state * state = (struct ggml_compute_state *) data;
64176443

64186444
const int n_threads = state->shared->n_threads;
@@ -6423,7 +6449,7 @@ void * ggml_graph_compute_thread(void * data) {
64236449
} else {
64246450
while (atomic_load(&state->shared->has_work)) {
64256451
if (atomic_load(&state->shared->stop)) {
6426-
return NULL;
6452+
return 0;
64276453
}
64286454
ggml_lock_lock (&state->shared->spin);
64296455
ggml_lock_unlock(&state->shared->spin);
@@ -6435,7 +6461,7 @@ void * ggml_graph_compute_thread(void * data) {
64356461
// wait for work
64366462
while (!atomic_load(&state->shared->has_work)) {
64376463
if (atomic_load(&state->shared->stop)) {
6438-
return NULL;
6464+
return 0;
64396465
}
64406466
ggml_lock_lock (&state->shared->spin);
64416467
ggml_lock_unlock(&state->shared->spin);
@@ -6454,7 +6480,7 @@ void * ggml_graph_compute_thread(void * data) {
64546480
}
64556481
}
64566482

6457-
return NULL;
6483+
return 0;
64586484
}
64596485

64606486
void ggml_graph_compute(struct ggml_context * ctx, struct ggml_cgraph * cgraph) {

ggml.h

+1
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ struct ggml_init_params {
136136
void * mem_buffer; // if NULL, memory will be allocated internally
137137
};
138138

139+
void ggml_time_init(void);
139140
int64_t ggml_time_ms(void);
140141
int64_t ggml_time_us(void);
141142
int64_t ggml_cycles(void);

msvc_thread_atomic.h

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#pragma once
2+
#include <Windows.h>
3+
4+
typedef volatile LONG atomic_int;
5+
typedef atomic_int atomic_bool;
6+
7+
static void atomic_store(atomic_int* ptr, LONG val) {
8+
InterlockedExchange(ptr, val);
9+
}
10+
static LONG atomic_load(atomic_int* ptr) {
11+
return InterlockedCompareExchange(ptr, 0, 0);
12+
}
13+
static LONG atomic_fetch_add(atomic_int* ptr, LONG inc) {
14+
return InterlockedExchangeAdd(ptr, inc);
15+
}
16+
static LONG atomic_fetch_sub(atomic_int* ptr, LONG dec) {
17+
return atomic_fetch_add(ptr, -(dec));
18+
}
19+
20+
typedef HANDLE pthread_t;
21+
22+
typedef DWORD thread_ret_t;
23+
static int pthread_create(pthread_t* out, void* unused, thread_ret_t(*func)(void*), void* arg) {
24+
out = CreateThread(NULL, 0, func, arg, 0, NULL);
25+
return out != NULL;
26+
}
27+
28+
static int pthread_join(pthread_t thread, void* unused) {
29+
return (int) WaitForSingleObject(thread, INFINITE);
30+
}
31+

whisper.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -2073,6 +2073,8 @@ bool log_mel_spectrogram(
20732073
//
20742074

20752075
struct whisper_context * whisper_init(const char * path_model) {
2076+
ggml_time_init();
2077+
20762078
whisper_context * ctx = new whisper_context;
20772079

20782080
const int64_t t_start_us = ggml_time_us();
@@ -2260,7 +2262,7 @@ struct whisper_full_params whisper_full_default_params(enum whisper_decode_strat
22602262
switch (strategy) {
22612263
case WHISPER_DECODE_GREEDY:
22622264
{
2263-
result = (struct whisper_full_params) {
2265+
result = {
22642266
.strategy = WHISPER_DECODE_GREEDY,
22652267
.n_threads = std::min(4, (int32_t) std::thread::hardware_concurrency()),
22662268
.offset_ms = 0,
@@ -2281,7 +2283,7 @@ struct whisper_full_params whisper_full_default_params(enum whisper_decode_strat
22812283
} break;
22822284
case WHISPER_DECODE_BEAM_SEARCH:
22832285
{
2284-
result = (struct whisper_full_params) {
2286+
result = {
22852287
.strategy = WHISPER_DECODE_GREEDY,
22862288
.n_threads = std::min(4, (int32_t) std::thread::hardware_concurrency()),
22872289
.offset_ms = 0,

0 commit comments

Comments
 (0)