Skip to content

Commit

Permalink
Merge pull request #1 from imaami/a_few_things
Browse files Browse the repository at this point in the history
Bug fixes and enhancements
  • Loading branch information
EmotionEngineer authored Oct 1, 2023
2 parents 2848d48 + c560bc1 commit d84ba96
Show file tree
Hide file tree
Showing 14 changed files with 4,555 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1 @@
*.c filter=lfs diff=lfs merge=lfs -text
/Lib/weights.c filter=lfs diff=lfs merge=lfs -text
13 changes: 13 additions & 0 deletions Lib/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
*.args.0
*.bc
*.d
*.dll
*.i
*.ii
*.lto_wrapper_args
*.ltrans.out
*.ltrans_args
*.o
*.res
*.s
*.so
53 changes: 53 additions & 0 deletions Lib/EmotionLib.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include <stdio.h>
#include <string.h>

#include "model.h"

__attribute__((always_inline))
static inline struct tensors *
tensors_create_helper (char const *const caller)
{
int e = 0;
struct tensors *t = tensors_create(&e);

if (!t) {
if (e)
fprintf(stderr, "%s: tensors_create(): %s\n", caller, strerror(e));
else
fprintf(stderr, "%s: tensors_create() failed\n", caller);
}

return t;
}

LIB_EXPORT void
EmotionLib_imageSentiment (EmotionLibImage const *image,
EmotionLibPrediction *result)
{
struct tensors *ts = tensors_create_helper(__func__);

if (ts) {
model_infer(ts, image, result);
tensors_destroy(&ts);
}
}

LIB_EXPORT void
EmotionLib_videoSentiment (EmotionLibImage const *frames,
int num_frames,
EmotionLibPrediction *results,
int *progress)
{
struct tensors *ts = tensors_create_helper(__func__);

if (ts) {
for (int i = 0; i < num_frames; i++) {
model_infer(ts, &frames[i], &results[i]);

if (progress)
(*progress)++;
}

tensors_destroy(&ts);
}
}
27 changes: 27 additions & 0 deletions Lib/EmotionLib.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef EMOTIONLIB_H_
#define EMOTIONLIB_H_

#if defined(_MSC_VER) || defined(_WIN32)
# define EMOTIONLIB_API_EXPORT __declspec(dllexport)
#else
# define EMOTIONLIB_API_EXPORT __attribute__((visibility("default")))
#endif

typedef float EmotionLibImage[3][224][224];
typedef float EmotionLibPrediction[4];

// Image inference
EMOTIONLIB_API_EXPORT void
EmotionLib_imageSentiment (EmotionLibImage const *image,
EmotionLibPrediction *result);

// Video inference
EMOTIONLIB_API_EXPORT void
EmotionLib_videoSentiment (EmotionLibImage const *frames,
int num_frames,
EmotionLibPrediction *results,
int *progress);

#undef EMOTIONLIB_API_EXPORT

#endif // EMOTIONLIB_H_
67 changes: 60 additions & 7 deletions Lib/Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,62 @@
# Compiler options
CC = gcc
CFLAGS = -shared -o emotionLib.dll -Wall -Werror -Ofast
override BIN := EmotionLib.dll
override SRC := EmotionLib.c activation.c model.c tensor.c weights.c
override OBJ := $(SRC:%=%.o)
override DEP := $(SRC:%=%.d)
override CC := $(or $(if $(filter default,$(origin CC)),,$(CC)),gcc)

# Source files
SOURCES = model.c
override undefine WITH_CLANG_CC
USE_CLANG = $(eval USE_CLANG := $$(shell \
printf '#ifdef __clang__\n1\n#endif\n' | $$(CC) -E -P -xc -))$(USE_CLANG)

all: $(SOURCES)
$(CC) $(CFLAGS) $(SOURCES)
override BASE_CFLAGS := $(strip -fPIC $(SANITIZE:%=-fsanitize=%) \
-O$(if $(DEBUG),$(if $(USE_CLANG),0,g) -g3,fast -DNDEBUG) -Wall -Wextra \
$(if $(USE_CLANG), \
-Weverything \
-Wno-c++-compat \
-Wno-c++98-compat \
-Wno-disabled-macro-expansion \
-Wno-fixed-enum-extension \
-Wno-gnu-empty-struct \
-Wno-gnu-statement-expression \
-Wno-gnu-statement-expression-from-macro-expansion \
-Wno-gnu-zero-variadic-macro-arguments \
-Wno-switch-enum \
-Wno-unknown-warning-option \
-Wno-unsafe-buffer-usage \
-Wno-used-but-marked-unused) \
-Wformat=2 -Wno-declaration-after-statement -Wno-unknown-pragmas)

ifeq (command line,$(origin CFLAGS))
override USER_CFLAGS := $(CFLAGS)
override EXTRA_CFLAGS := $(strip \
$(if $(filter -save-temps,$(USER_CFLAGS)), \
$(if $(USE_CLANG), \
-Wno-constant-logical-operand \
-Wno-gnu-line-marker -pipe), \
-pipe) \
$(filter-out -flto -pipe,$(USER_CFLAGS)))
else
override undefine USER_CFLAGS
override EXTRA_CFLAGS := -pipe
endif

ifeq (,$(filter -fno-lto,$(USER_CFLAGS)))
override BASE_CFLAGS := -flto $(BASE_CFLAGS)
endif

override CFLAGS := $(BASE_CFLAGS) $(EXTRA_CFLAGS)
override LIBS := -lm

$(BIN): $(OBJ)
$(CC) -shared $(CFLAGS) -o $@ $^ $(LIBS)

%.c.o: %.c
$(CC) $(CFLAGS) -o $@ -c -I. -MMD -MQ $@ -MF $<.d $<

-include $(DEP)

.PHONY: clean
clean: override WHAT = $(wildcard \
$(BIN) $(OBJ) $(DEP) *.args.0 *.bc *.i *.ii *.lto_wrapper_args \
*.ltrans_args *.ltrans0.o *.ltrans.o *.ltrans.out *.res *.s *.temp.o)
clean:; $(if $(WHAT),$(RM) $(WHAT),@:)
Loading

0 comments on commit d84ba96

Please sign in to comment.